练习7.1 使用2.6.1节定义的Sales_data类为1.6节的交易处理程序编写一个新版本。
解:
#include <iostream>
#include <string>
using std::cin; using std::cout; using std::endl; using std::string;
struct Sales_data
{
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main()
{
Sales_data total;
if (cin >> total.bookNo >> total.units_sold >> total.revenue)
{
Sales_data trans;
while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue)
{
if (total.bookNo == trans.bookNo)
{
total.units_sold += trans.units_sold;
total.revenue += trans.revenue;
}
else
{
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
total = trans;
}
}
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}
练习7.2 曾在2.6.2节的练习中编写了一个Sales_data类,请向这个类添加combine函数和isbn成员。
解:
#include <string>
struct Sales_data {
std::string isbn() const { return bookNo; };
Sales_data& combine(const Sales_data&);
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
练习7.3 修改7.1.1节的交易处理程序,令其使用这些成员。
解:
#include <iostream>
using std::cin; using std::cout; using std::endl;
int main()
{
Sales_data total;
if (cin >> total.bookNo >> total.units_sold >> total.revenue)
{
Sales_data trans;
while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue) {
if (total.isbn() == trans.isbn())
total.combine(trans);
else {
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
total = trans;
}
}
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}
练习7.4 编写一个名为Person的类,使其表示人员的姓名和地址。使用string对象存放这些元素,接下来的练习将不断充实这个类的其他特征。
解:
#include <string>
class Person {
std::string name;
std::string address;
};
练习7.5 在你的Person类中提供一些操作使其能够返回姓名和地址。 这些函数是否应该是const的呢?解释原因。
解:
#include <string>
class Person
{
std::string name;
std::string address;
public:
auto get_name() const -> std::string const& { return name; }
auto get_addr() const -> std::string const& { return address; }
};
应该是const的。因为常量的Person对象也需要使用这些函数操作。
练习7.6 对于函数add、read和print,定义你自己的版本。
解:
#include <string>
#include <iostream>
struct Sales_data {
std::string const& isbn() const { return bookNo; };
Sales_data& combine(const Sales_data&);
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
// member functions.
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
// nonmember functions
std::istream &read(std::istream &is, Sales_data &item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
}
std::ostream &print(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue;
return os;
}
Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
练习7.7 使用这些新函数重写7.1.2节练习中的程序
int main()
{
Sales_data total;
if (read(std::cin, total))
{
Sales_data trans;
while (read(std::cin, trans)) {
if (total.isbn() == trans.isbn())
total.combine(trans);
else {
print(std::cout, total) << std::endl;
total = trans;
}
}
print(std::cout, total) << std::endl;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}
练习7.8 为什么read函数将其Sales_data参数定义成普通的引用,而print函数将其参数定义成常量引用?
解: 因为read函数会改变对象的内容,而print函数不会。
练习7.9 对于7.1.2节练习中代码,添加读取和打印Person对象的操作。
#include <string>
#include <iostream>
struct Person
{
std::string const& getName() const { return name; }
std::string const& getAddress() const { return address; }
std::string name;
std::string address;
};
std::istream &read(std::istream &is, Person &person)
{
return is >> person.name >> person.address;
}
std::ostream &print(std::ostream &os, const Person &person)
{
return os << person.name << " " << person.address;
}
练习7.10 在下面这条if语句中,条件部分的作用是什么?
if (read(read(cin, data1), data2)) //等价read(std::cin, data1);read(std::cin, data2);
read函数的返回值是istream对象, if语句中条件部分的作用是从输入流中读取数据给两个data对象。