#include#include #include #include #include using namespace std; class Sales_data { friend std::istream& operator>>(std::istream&, Sales_data&); friend std::ostream& operator<<(std::ostream&, const Sales_data&); friend Sales_data operator+(const Sales_data&,const Sales_data&);//非成員函數(對稱性) friend bool operator==(const Sales_data&,const Sales_data&); friend bool operator!=(const Sales_data&,const Sales_data&); public: Sales_data():units_sold(0),revenue(0.0){} Sales_data(const std::string &s):bookNo(s),units_sold(0),revenue(0.0){} Sales_data(const std::string &s,unsigned n,double p):bookNo(s),units_sold(n),revenue(n*p){} Sales_data(std::istream &is); Sales_data& operator+=(const Sales_data&);//成員函數(復合賦值一般是成員函數) std::string isbn()const{return bookNo;} Sales_data& operator=(const std::string &s); private: inline double avg_price()const;//inline private: std::string bookNo; //isbn unsigned units_sold;//銷售量 double revenue; //總價 }; inline double Sales_data::avg_price() const { return units_sold ? revenue/units_sold : 0; } Sales_data::Sales_data(std::istream &is) { is>>*this; } std::istream& operator>>(std::istream& is,Sales_data& item) { double price=0.0; is>>item.bookNo>>item.units_sold>>price; if(is)//檢測流的狀態,輸入易錯且改寫對象數據 item.revenue=item.units_sold*price; else item=Sales_data(); return is; } std::ostream& operator<<(std::ostream& os, const Sales_data& item) { os<