C++primer 7.1.2 sessions

Source: Internet
Author: User

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

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.