这是一个关于表示一本书的总销售额、售出册数和平均售价的类的定义。
有详细的注释,包含友元函数、运算符重载和内联函数的定义及使用的部分内容。
/*
* 头文件 SALES_ITEM.H
* 定义了类 Sales_item , 包含了类的声明,成员变量成员函数的定义。
* 该类中使用了友元函数 friend ,友元函数的定义在类的定义之外。
* 还使用了运算符重载 operator ,运算符重载的定义也在类的定义之外。
*/
#ifndef SALESITEM_H
// if no define 检测 SALESITEM_H 是否已经被定义,如果没有被定义,则定义它
#define SALESITEM_H
// difine 定义 SALESITEM_H 等同于 Slas_item.h
// 该类的声明和相关函数的声明在这里
#include <iostream>
// iostream 标准输入输出流
#include <string>
// string 字符串
class Sales_item {
// 定义类 Sales_item
friend std::istream& operator>>(std::istream&, Sales_item&);
// 使用友元函数 friend 来声明重载运算符 >> 的函数原型。
// 友元函数的概念:友元函数是类的成员函数,但是不是类的成员函数的一部分。
// 友元函数的定义在类的定义之外,但是可以访问类的私有成员。
// 友元函数的声明在类的定义之内,但是不是类的成员函数的一部分。
// 运算符重载:operator >>
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
friend bool operator<(const Sales_item&, const Sales_item&);
friend bool operator==(const Sales_item&, const Sales_item&);
public:
// constructors are explained in section 7.1.4, pages 262 - 265
// default constructor needed to initialize members of built-in type
Sales_item(): units_sold(0), revenue(0.0) { }
// 默认构造函数,初始化成员变量 units_sold 和 revenue
Sales_item(const std::string &book):
bookNo(book), units_sold(0), revenue(0.0) { }
// 构造函数,接受一个 string 类型的参数 book ,并且初始化 bookNo 为 book
Sales_item(std::istream &is) { is >> *this; }
// 构造函数,接受一个 istream 类型的参数 is ,
// 并且调用 istream 类型的运算符重载 >> ,将 is 赋值给 *this
public:
// Sales_item 类对象的运作
Sales_item& operator+=(const Sales_item&);
// 成员二元运算符重载,左操作数绑定到隐式的 this 指针
// operations on Sales_item objects
// 类Sales_item对象的运算
std::string isbn() const { return bookNo; }
double avg_price() const;
// 之前的私有成员
private:
std::string bookNo; // 隐式的初始化bookNo 为string类型的空串
unsigned units_sold; // 显式的初始化units_sold 为 0
double revenue; // 显式的初始化revenue 为 0.0
};
// 内联函数
inline
bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
{ return lhs.isbn() == rhs.isbn(); }
// 比较书号
// nonmember binary operator: must declare a parameter for each operand
Sales_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.isbn() == rhs.isbn();
}
// 内联重载运算符 == 的方法, 接受两个类对象作为参数,返回一个 bool 类型的值
inline bool
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
return !(lhs == rhs); // != defined in terms of operator==
}
// 内联重载运算符 != 的方法, 接受两个类对象作为参数,返回一个 bool 类型的值
// 用来判断两个类对象的书号是否相同
// assumes that both objects refer to the same ISBN
Sales_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 ISBN
Sales_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
}
// 重载运算符 + 的方法,接受两个类对象作为参数,返回一个类对象
// 用来将两个类对象的书号相同的对象的销售量和销售额相加
std::istream& operator>>(std::istream& in, Sales_item& s)
{
double price;
in >> s.bookNo >> 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;
}
std::ostream& operator<<(std::ostream& out, const Sales_item& s)
{
out << s.isbn() << " " << s.units_sold << " "
<< s.revenue << " " << s.avg_price();
return out;
}
double Sales_item::avg_price() const
{
if (units_sold)
return revenue/units_sold;
else
return 0;
}
#endif
这是主函数,用来简单使用类定义对象。
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item1, item2;
std::cin >> item1 >> item2; //read a pair of transactions
std::cout << item1 + item2 << std::endl; //print their sum
return 0;
}
// Description: calculate the average price of a book
// 计算书籍的平均价格
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item total; // variable to hold data for the next transaction
// 对象 total 用来保存下一个交易记录的数据
// read the first transaction and ensure that there are data to process
if (std::cin >> total) {
Sales_item trans; // variable to hold the running sum
// 对象trans 用来保存当前求和的结果
// read and process the remaining transactions
while (std::cin >> trans) {
// if we're still processing the same book
if (total.isbn() == trans.isbn())
total += trans; // update the running total
else {
// print results for the previous book
std::cout << total << std::endl;
total = trans; // total now refers to the next book
// 重新赋值,total指向下一本书
}
}
std::cout << total << std::endl; // print the last transaction
} else {
// no input! warn the user
std::cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
return 0;
}
// 这段代码的主要功能是将多个ISBN相同的书籍的销售记录合并成一条记录。
// 例如,输入:
// 0-201-78345-X 3 20.00
// 0-201-78345-X 2 25.00
// 0-201-78345-X 3 22.50
// 0-201-78345-X 1 30.00
// 0-201-78345-X 2 35.00
// 输出:
// 0-201-78345-X 11 24.55
文章描述了一个C++类Sales_item,用于表示书籍销售数据,包括书号、售出册数和总收入。类中使用了友元函数处理输入输出,如istream和ostream的重载,以及比较运算符。同时,类还支持运算符重载,如`+`和`+=`,实现对相同ISBN的销售记录进行合并和计算平均价格的功能。

371

被折叠的 条评论
为什么被折叠?



