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