"c + + primer, 5e"

Source: Internet
Author: User

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.