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.