Practice 7,2
1#include <iostream>2#include <string>3 using namespacestd;4 5 structSales_data {6 stringBookno;7 unsigned units_sold;8 DoublePrice =0.0;9 DoubleRevenue =0.0;TenSales_data &combine (Constsales_data&); One stringISBN ()Const{returnBookno;} A }; - -Sales_data & Sales_data::combine (ConstSales_data &RHS) the { -Units_sold + =Rhs.units_sold; -Revenue + =rhs.revenue; - return* This; + } - + intMain () A { at Sales_data item1, item2; - DoubleTotalrevenue =0; - DoubleTotalsold =0; - intCounter =1; - if(Cin >> Item1.bookno >> item1.units_sold >>Item1.price) - { inItem1.revenue = Item1.price *Item1.units_sold; - while(Cin >> Item2.bookno >> item2.units_sold >>item2.price) { toItem2.revenue = Item2.price *Item2.units_sold; + if(Item1.bookno = =item2.bookno) { - Item1.combine (item2); the++counter; * } $ Else {Panax Notoginsengcout << ITEM1.ISBN () <<" "<< Item1.units_sold <<" "<< item1.revenue <<"Times :"<< counter <<Endl; -Item1.bookno =Item2.bookno; theItem1.units_sold =Item2.units_sold; +Item1.revenue =item2.revenue; ACounter =1; the } + } -cout << Item1.bookno <<" "<< Item1.units_sold <<" "<< item1.revenue <<"Times :"<< counter <<Std::endl; $ } $ return 0; -}
Exercise 7.3
See above code
Exercise 7.4
1 struct Person {2 string person_name; // Name of person 3 string person_add; // address of person in residence 4 };
Exercise 7.5
1 string Const return Person_name;} 2 string Const return person_addr; }
Should be const, the object referred to in this is not changed in the body of the two functions, so setting this to a pointer to a constant will help increase the flexibility of the function.
C++primer 7.1.2 sessions