Exercise 7.41
1#include <iostream>2#include <string>3#include"Factmain.h"4 using namespacestd;5 6 structSales_data {7Friend Ostream &print (Ostream &os,ConstSales_data &item);8Friend IStream &read (IStream & is, Sales_data &item);9 Private:Ten stringBookno; One unsigned units_sold; A DoublePrice ; - Doublerevenue; - DoubleAvg_price ()Const; the Public: -Sales_data (Const string&s,ConstUnsigned &u,Const Double&P): Bookno (s), Units_sold (U), Price (p) {cout <<" One"<<Endl;} -Sales_data (): Sales_data ("12345",3, A) {cout <<"using a delegate constructor to construct a default constructor"<<Endl;} -Sales_data (strings): Sales_data (s),2,4) {cout <<"just enter a string"<<Endl;} + //sales_data (const string &s, const unsigned &u): Bookno (s), Units_sold (u) {} -Sales_data (IStream & is): Sales_data () {Read ( is, * This); cout <<"One last"<<Endl;} + stringISBN ()Const{returnBookno;} AUnsigned &runit () {returnUnits_sold;} at Double&rprice () {returnPrice ;} - Double&rrevenue () {returnrevenue;} -Sales_data &combine (Constsales_data&); - - }; - inSales_data Add (ConstSales_data &LHS,ConstSales_data &RHS); -Ostream &print (Ostream &os,ConstSales_data &item); toIStream &read (IStream & is, Sales_data &item); + - the intMain ()//the part of the main function needs to be changed * { $ sales_data S1;Panax NotoginsengSales_data S2 ("123",2,3); -Sales_data S3 (" A"); the Sales_data S4 (CIN); + /*sales_data test1 ("Hello", "the"); A print (cout, test1); the sales_data item1 (CIN); + Sales_data item2; - double totalrevenue = 0; $ double totalsold = 0; $ int counter = 1; - if ((ITEM1.ISBN ()). empty ()) - { the item1.rrevenue () = Item1.rprice () * Item1.runit (); - While (read (cin,item2)) {Wuyi item2.rrevenue () = Item2.rprice () * Item2.runit (); the if (item1.isbn () = = ITEM2.ISBN ()) { - Item1.combine (item2); Wu ++counter; - } About else { $ print (cout, item1) << Endl; - item1.isbn () = ITEM2.ISBN (); - item1.runit () = Item2.runit (); - item1.rrevenue () = Item2.rrevenue (); A counter = 1; + } the } - print (cout, item2) << Endl; $ }*/ theSystem"Pause"); the return 0; the } the -Sales_data Add (ConstSales_data & LHS,ConstSales_data & RHS)//non-member function add in { theSales_data sum =LHS; the Sum.combine (RHS); About returnsum; the //TODO: Insert a return statement here the } the +IStream & Read (IStream & is, Sales_data &Item) - { the is>> item.bookno >> item.units_sold >>Item.price;BayiItem.revenue = Item.units_sold *Item.price; the return is; the //TODO: Insert a return statement here - } - theOstream & Print (Ostream & OS,ConstSales_data &Item) the { theOS << item.bookno <<" "<< Item.units_sold <<" "<< Item.price <<" "<<item.revenue; the returnos; - //TODO: Insert a return statement here the } the theSales_data & Sales_data::combine (ConstSales_data &RHS)94 { theUnits_sold + =Rhs.units_sold; theRevenue + =rhs.revenue; the return* This;98 } About -InlineDoubleSales_data::avg_price ()Const101 {102 returnUnits_sold? Revenue/units_sold:0;103}
Exercise 7.42
1#include <iostream>2#include <string>3 using namespacestd;4 5 classDate {6 Public:7Date (unsigned y, unsigned m, unsigned d,Const string&s): Years (y), Mon (m), days (d), weather (s) {};8Date (): Date ( -,1,1,"Good") {}9Date (string&s): Date (1999,9,9, s) {}Ten One Private: A unsigned years; - unsigned mon; - unsigned days; the stringweather; -};
C++primer 7.5.2 sessions