C++など-1

1944 ワード

#include
#include

using namespace std;

class Sales_items
{
public:
    Sales_items(std::string &book, unsigned units, double amount)
        :isbn(book), units_sold(units), revenue(amount)
    {}

    //      
    double avg_price() const
    {
        if (units_sold) 
            return revenue / units_sold;
        else
            return 0;

    }

    //           
    bool same_isbn(const Sales_items &rhs) const
    {
        return isbn == rhs.isbn;
    }

    void add(const Sales_items &rhs)
    {
        units_sold += rhs.units_sold;
        revenue += rhs.revenue;
    }

private:
    std::string isbn;   //  
    unsigned units_sold;//    
    double revenue;     //   
};

class Person
{
    //  
public:
    /*Person(const std::string &nm,const std::string &addr)
    {
        name = nm;
        address = addr;
    }*/

    //                            ,    
    Person(const std::string &nm, const std::string &addr) :name(nm), address(addr)
    {

    }
    std::string getName() const//   const             ,         ,     
    {
        //                 
        return name;
    }
    std::string getAdress() const
    {
        return address;
    }

private:
    std::string name;
    std::string address;
};

int main()
{
    Person a("zhangfei","garden street");

    //cout << a.name << "," << a.address;
    cout << a.getName() << "," << a.getAdress() << endl;

    Sales_items x(string("0-399-82477-1"), 2, 20.00);
    Sales_items y(string("0-399-82477-1"), 6, 48.00);
    cout << x.avg_price() << endl;
    cout << y.avg_price() << endl;
    if (x.same_isbn(y))
        x.add(y);

    cout << "           :"<