博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
习题 1.23 sale_item 基本操作 类
阅读量:5173 次
发布时间:2019-06-13

本文共 3069 字,大约阅读时间需要 10 分钟。

#include
#include"Sales_item.h"using namespace std;int main(){ Sales_item book,sum; cout<<"intput the book:isbn+count+price"<
>sum; while(cin>>book) { if(!sum.same_isbn(book)) sum += book; } cout<
<
#include
class Sales_item {friend bool operator==(const Sales_item&, const Sales_item&);// other members as beforepublic: // added constructors to initialize from a string or an istream Sales_item(const std::string &book): isbn(book), units_sold(0), revenue(0.0) { } Sales_item(std::istream &is) { is >> *this; } friend std::istream& operator>>(std::istream&, Sales_item&); friend std::ostream& operator<<(std::ostream&, const Sales_item&);public: // operations on Sales_item objects // member binary operator: left-hand operand bound to implicit this pointer Sales_item& operator+=(const Sales_item&); // other members as before public: // operations on Sales_item objects double avg_price() const; bool same_isbn(const Sales_item &rhs) const { return isbn == rhs.isbn; } // default constructor needed to initialize members of built-in type Sales_item(): units_sold(0), revenue(0.0) { }// private members as beforeprivate: std::string isbn; unsigned units_sold; double revenue;};// nonmember binary operator: must declare a parameter for each operandSales_item operator+(const Sales_item&, const Sales_item&);inline bool operator==(const Sales_item &lhs, const Sales_item &rhs){ // must be made a friend of Sales_item return lhs.units_sold == rhs.units_sold && lhs.revenue == rhs.revenue && lhs.same_isbn(rhs);}inline bool operator!=(const Sales_item &lhs, const Sales_item &rhs){ return !(lhs == rhs); // != defined in terms of operator==}using std::istream; using std::ostream;// assumes that both objects refer to the same isbninlineSales_item& Sales_item::operator+=(const Sales_item& rhs) { units_sold += rhs.units_sold; revenue += rhs.revenue; return *this;}// assumes that both objects refer to the same isbninlineSales_item operator+(const Sales_item& lhs, const Sales_item& rhs) { Sales_item ret(lhs); // copy lhs into a local object that we'll return ret += rhs; // add in the contents of rhs return ret; // return ret by value}inlineistream& operator>>(istream& in, Sales_item& s){ double price; in >> s.isbn >> s.units_sold >> price; // check that the inputs succeeded if (in) s.revenue = s.units_sold * price; else s = Sales_item(); // input failed: reset object to default state return in;}inlineostream& operator<<(ostream& out, const Sales_item& s){ out << s.isbn << "\t" << s.units_sold << "\t" << s.revenue << "\t" << s.avg_price(); return out;}inlinedouble Sales_item::avg_price() const{ if (units_sold) return revenue/units_sold; else return 0;}#endif

  

转载于:https://www.cnblogs.com/chhyong88/archive/2011/11/29/2268439.html

你可能感兴趣的文章
88. 合并两个有序数组
查看>>
ASP.NET MVC 范例推荐
查看>>
mysql 笔记
查看>>
(zt)透析回调函数
查看>>
[Android]自定义控件LoadMoreRecyclerView
查看>>
W:Failed to fetch http://archive.ubuntukylin.com:10006/ubuntukylin/dists/pre
查看>>
根据条件对DataTable进行Distinct操作
查看>>
关于制作赛车游戏的一些入门知识(一)
查看>>
AI 语音对话技术
查看>>
go语言基础之有参有返回值函数的使用
查看>>
C# 监听HTTP请求(遇到的一些问题)
查看>>
MapReduce编程初步(WordCount,TopN)
查看>>
Mybatis #和$区别
查看>>
MySQL 数据底部出现总计字样 第二种办法 纵向合并 20161103
查看>>
实验三
查看>>
第二十三模板 11模板参数提供默认值
查看>>
【转】32位plsql连接64位oracle
查看>>
python 抓取,解析页面(网站汇总)
查看>>
js校验json格式
查看>>
第一天,来报道
查看>>