《C++ Primer》5th 课后练习 第七章 类 1~10

练习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对象。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值