Practice
7.6 & 7.7
#include <iostream>#include<string>using namespacestd;//sales_data.hstructSales_data {//public section, What does the object look Like. std::stringISBN ()Const{returnbookno; }//Inline Functionsales_data& Combine (Constsales_data&); DoubleAvg_price ()Const; //Private section, Data member. std::stringbookno; unsigned units_sold=0; DoubleRevenue =0.0;};//Non-member interface functions for Sales_dataSales_data Add (Constsales_data&,Constsales_data&); Std::ostream&print (std::ostream&,Constsales_data&); Std::istream&read (std::iostream&, sales_data&);//Sales_data.cppsales_data& Sales_data::combine (ConstSales_data &Rhs) {units_sold+=rhs.units_sold; Revenue+=rhs.revenue; return* this;} IStream&read (istream & is, Sales_data &Item) { DoublePrice =0; is>> item.bookno >> item.units_sold >>price ; Item.revenue= Price *item.units_sold; return is;} Ostream&print (ostream &os,ConstSales_data &Item) {os<< ITEM.ISBN () <<" "<< Item.units_sold <<" "<< item.revenue <<" "<<Item.avg_price (); returnos;}DoubleSales_data::avg_price ()Const { if(units_sold)returnrevenue/units_sold; Else return 0;} Sales_data Add (ConstSales_data &lhs,ConstSales_data &Rhs) {sales_data sum=lhs; Sum.combine (rhs); returnsum;}//main.cppintmain () {sales_data total; if(read (cin, total)) {sales_data trans; while(read (cin, TRANS)) {if(TOTAL.ISBN () = =TRANS.ISBN ()) total.combine (trans); Else{print (cout, Total)<<endl; total=trans; }} Print (cout, Total)<<endl; } Else{cerr<<"No data?!"<<endl; } return 0;}
7.8
The read operation alters the data members of the object, while the print operation is simply "access" without altering the Object's data members.
"c + + primer, 5e"