Exercise 7.11, Exercise 7.13, exercise 7.14
1#include <iostream>2#include <string>3#include"Factmain.h"4 using namespacestd;5 6 structSales_data {7 stringISBN ()Const{returnBookno;}8Sales_data &combine (Constsales_data&);9 stringBookno;Ten unsigned units_sold; One DoublePrice =0.0; A DoubleRevenue =0.0; -Sales_data () =default; -Sales_data (Const string&s,ConstUnsigned &u,Const Double&p): Bookno (s), Units_sold (U), Price (p) {}; theSales_data (Const string&s,ConstUnsigned &u): Bookno (s), Units_sold (u) {}; -Sales_data (IStream & is); - }; - +Sales_data Add (ConstSales_data &LHS,ConstSales_data &RHS); -Ostream &print (Ostream &os,ConstSales_data &item); +IStream &read (IStream & is, Sales_data &item); A at - intMain () - { -Sales_data Test1 ("Hello", About); - print (cout, test1); - sales_data item1 (CIN); in Sales_data item2; - DoubleTotalrevenue =0; to DoubleTotalsold =0; + intCounter =1; - if((item1.bookno). Empty ()) the { *Item1.revenue = Item1.price *Item1.units_sold; $ while(Read (cin,item2)) {Panax NotoginsengItem2.revenue = Item2.price *Item2.units_sold; - if(Item1.bookno = =item2.bookno) { the Item1.combine (item2); +++counter; A } the Else { +Print (cout, item1) <<Endl; -Item1.bookno =Item2.bookno; $Item1.units_sold =Item2.units_sold; $Item1.revenue =item2.revenue; -Counter =1; - } the } -Print (cout, item2) <<Endl;Wuyi } theSystem"Pause"); - return 0; Wu } - AboutSales_data Add (ConstSales_data & LHS,ConstSales_data & RHS)//non-member function add $ { -Sales_data sum =LHS; - Sum.combine (RHS); - returnsum; A //TODO: Insert a return statement here + } the -IStream & Read (IStream & is, Sales_data &Item) $ { the is>> item.bookno >> item.units_sold >>Item.price; theItem.revenue = Item.units_sold *Item.price; the return is; the //TODO: Insert a return statement here - } in theOstream & Print (Ostream & OS,ConstSales_data &Item) the { AboutOS << item.bookno <<" "<< Item.units_sold <<" "<< Item.price <<" "<<item.revenue; the returnos; the //TODO: Insert a return statement here the } + -Sales_data & Sales_data::combine (ConstSales_data &RHS) the {BayiUnits_sold + =Rhs.units_sold; theRevenue + =rhs.revenue; the return* This; - } - theSales_data::sales_data (IStream & is) the { theRead is, * This); the}
Exercise 7.12
Need to use friend function, to be solved
Exercise 7.15
See Final procedure
C++primer 7.1.4 sessions