【C++】【学习笔记】向自定类中添加 combine 和 isbn 成员

来源:互联网 发布:服务网络占用硬盘 编辑:程序博客网 时间:2024/06/01 09:14
/*类添加 combine 和 isbn 成员代码如下:*/#include <iostream>  #include <string>  class Sales_data{private:// 私有数据成员std::string BookNo;// 书籍编号,隐士初始化为空串unsigned units_sold;// 销售量,显示初始化为0double SellingPrice;// 原始价格,显示初始化为0.0double SalePrice;// 实售价格,显示初始化为0.0double Discount;// 折扣,显示初始化为0.0public:// isbn(International Standard Book Number)函数只有一条语句,返回 BookNostd::string isbn() const{return BookNo;}// combine 函数用于把两个 ISBN 相同的销售记录合并在一起Sales_data& combine( const Sales_data &rhs )// rhs:right hand side指的是 == 运算符右边的操作数{units_sold += rhs.units_sold;// 累加书籍销售量SalePrice = (rhs.SalePrice * rhs.units_sold + SalePrice * units_sold)  / (rhs.units_sold + units_sold);// 实售价格 = 总售价/总销售册数if (SellingPrice != 0)Discount = SalePrice / SellingPrice;// 计算折扣return *this;}};

阅读全文
0 0
原创粉丝点击