C ++ primer bookstore program, primer bookstore Program

Source: Internet
Author: User

C ++ primer bookstore program, primer bookstore Program

# Include <iostream> # include <string> # include <cassert> # include <algorithm> # include <vector> using namespace std; class Sales_data {friend std :: istream & operator> (std: istream &, Sales_data &); friend std: ostream & operator <(std: ostream &, const Sales_data &); friend Sales_data operator + (const Sales_data &, const Sales_data &); // non-member function (Symmetry) friend bool operator = (const Sales_data &, const Sales_data &); frien D bool operator! = (Const Sales_data &, const Sales_data &); public: Sales_data (): units_sold (0), revenue (0.0) {} Sales_data (const std: string & s ): bookNo (s), units_sold (0), revenue (0.0) {}sales_data (const std: string & s, unsigned n, double p): bookNo (s ), units_sold (n), revenue (n * p) {} Sales_data (std: istream & is); Sales_data & operator + = (const Sales_data &); // member function (composite assignment is generally a member function) std: string isbn () const {return bookNo;} Sales_data & ope Rator = (const std: string & s); private: inline double avg_price () const; // inlineprivate: std: string bookNo; // isbn unsigned units_sold; // sales volume double revenue; // total price}; inline double Sales_data: avg_price () const {return units_sold? Revenue/units_sold: 0;} Sales_data: Sales_data (std: istream & is) {is> * this;} std: istream & operator> (std :: istream & is, Sales_data & item) {double price = 0.0; is> item. bookNo> item. units_sold> price; if (is) // checks the stream status. The input is error-prone and the object data item is rewritten. revenue = item. units_sold * price; else item = Sales_data (); return is;} std: ostream & operator <(std: ostream & OS, const Sales_data & item) {OS <item. isbn () <"" <item. units_sold <"" <Item. revenue <"<item. avg_price (); return OS;} Sales_data operator + (const Sales_data & item1, const Sales_data & item2) {Sales_data s (item1); s + = item2; // call the compound value return s;} Sales_data & Sales_data: operator + = (const Sales_data & rhs) {units_sold + = rhs. units_sold; revenue + = rhs. revenue; return * this;} bool operator = (const Sales_data & lhs, const Sales_data & rhs) {return lhs. bookNo = rhs. bookNo & lhs. un Its_sold = rhs. units_sold & lhs. revenue = rhs. revenue;} bool operator! = (Const Sales_data & lhs, const Sales_data & rhs) {return! (Lhs = rhs);} Sales_data & Sales_data: operator = (const std: string & s) {* this = Sales_data (s); return * this ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.