7.1节练习

学习第十二章前,再温习一下第七章。( = 。= 忘得差不多了

7.1 使用2.6.1节定义的Sales_data类为1.6节(第21页)的交易处理程序编写一个新版本。

#include <iostream>
#include "Sales_data.h"
#include <string>
using namespace std;
int main()
{
	Sales_data total;
	//输入三个数据 book_no, book_sold, revenue.
	if (cin >> total.book_no >> total.book_sold >> total.revenue) {
		Sales_data trans;
		while (cin >> trans.book_no >> trans.book_sold >> trans.revenue) {
			if (total.book_no == trans.book_no) {
				total.book_sold += trans.book_sold;
				total.revenue += trans.revenue;
			}else{
				cout << total.book_no << ": " << total.book_sold << " " << total.revenue << endl;
				total = trans;
			}
		}
		cout << total.book_no << ": " << total.book_sold << " " << total.revenue << endl;
	}
	else {
		cerr << "no data.";
		return -1;
	}
	return 0;
}


7.2 曾在2.6.2节的练习(第67页)中辫子额了一个Sales_data类,请向这个类添加combine和isbn成员。

struct Sales_data
{
	Sales_data & combine(Sales_data &);
	std::string isbn() const { return book_no };
	std::string book_no ;
	unsigned book_sold = 0;
	double revenue = 0.0;
};
Sales_data & Sales_data::combine(Sales_data & rhs) {
	book_sold += rhs.book_sold;
	revenue += rhs.revenue;
	return *this;
}

7.3 修改7.1.1节(第229页)的交易处理程序,令其使用这些成员。

#include <iostream>
#include "Sales_data.h"
#include <string>
using namespace std;
int main()
{
	Sales_data total;
	if (cin >> total.book_no >> total.book_sold >> total.revenue) {
		Sales_data trans;
		while (cin >> trans.book_no >> trans.book_sold >> trans.revenue) {
			if (total.isbn() == trans.isbn()) {
				total.combine(trans);
			}
			else {
				cout << total.book_no << ": " << total.book_sold << " " << total.revenue << endl;
				total = trans;
			}
		}
		cout << total.book_no << ": " << total.book_sold << " " << total.revenue << endl;
	}
	else {
		cerr << "no data.";
		return -1;
	}
	return 0;
}

练习7.4 编写一个名为Personde 类,使其表示人员的姓名和住址。使用string对象存放这些元素,接下来的练习将部队充实这个类的其他特征。

struct Person
{
	string name;
	string adress;
};

练习7.5 在你的Person类中提供一些操作使其能够返回姓名和住址,这些函数是否应该是const的呢?解释原因。

函数应该是常量成员函数,因为若调用成员函数的对象是一个常量,则会类型不适用。

struct Person
{
	std::string isname() const { return name; };
	std::string isadress() const { return adress; };

	std::string name;
	std::string adress;
};

练习7.6 对于函数add、read和print,定义你自己的版本。

struct Sales_data
{
	Sales_data & combine(Sales_data &);
	std::string isbn() const { return book_no; };
	double avg_price()const;

	std::string book_no ;
	unsigned book_sold = 0;
	double revenue = 0.0;
};
double Sales_data::avg_price()const
{
	double avg = 0;
	if (book_sold) {
		avg = revenue / book_sold;
	}
	return avg;
}
Sales_data & Sales_data::combine(Sales_data & rhs) {
	book_sold += rhs.book_sold;
	revenue += rhs.revenue;
	return *this;
}
Sales_data add(const Sales_data &rhs, const Sales_data &lhs)
{
	Sales_data sum = rhs;
	sum.combine(rhs);
	return sum;
}
std::istream &read(std::istream is, Sales_data &item)
{
	double price = 0;
	is >> item.book_no >> item.book_sold >> price;
	item.revenue = item.book_sold * price;
	return is;
}
std::ostream &print(std::ostream, const Sales_data &item)
{
	os << item.book_no << ": " << item.book_sold << " " << item.revenue << " " << item.avg_price();
	return os;
}

练习7.8 为什么read函数将其Sales_data参数定义成普通的引用,而print将其参数定义成常量的引用。

因为read函数中修改了对象的数据revenue,而print函数中没有。


练习7.9 对于7.1.2节(第233页)练习中的代码,添加读取和打印Person对象的操作。

#include <iostream>
#include <string>
struct Person
{
	std::string isname() const { return name; };
	std::string isadress() const { return adress; };

	std::string name;
	std::string adress;
};
std::istream &read(std::istream &is, Person &m)
{
	is >> m.name >> " " >> m.adress;
	return is;
}
std::ostream &print(std::ostream &os, const Person &m)
{
	os << m.isname()<< " " << m.isadress();
	return os;
}

练习7.10 在下面这条if语句中,条件部分的作用是什么?

if(read((read(cin,data1)),data2))

答:read(cin,data1)返回的是istream& 输入流,

       read((read(cin,data1)),data2)返回的也是istream& 输入流。

所以条件部分检测的是是否将data2的数据输入data1。


练习7.11 在你的Sales_data类中添加构造函数,然后编写一段程序令其用到每个构造函数。

#include <iostream>
#include <string>
#include "Sales_data.h"

using namespace std;
int main()
{
	Sales_data item1;
	Sales_data item2("C++primer");
	Sales_data item3("CprimerPlus", 2, 40.2);
	Sales_data item4(cin);

	print(cout, item1) << endl;
	
	print(cout, item2) << endl;
	
	print(cout, item3) << endl;

	print(cout, item4) << endl;
	
}


练习7.12 把只接受一个istream作为参数的构造函数定义移动到类的内部。

Sales_data(std::istream &is)
	{
		double price = 0;
		is >> book_no >> book_sold >> price;
		revenue = price*book_sold;
	}

7.13 使用istream构造函数重写第229页的程序。

#include <iostream>
#include <string>
#include "Sales_data.h"
using namespace std;
int main()
{
	Sales_data total;
	if (read(cin,total)) {
		Sales_data trans;
		while (read(cin, trans)) {
			if (total.isbn() == trans.isbn()) {
				total=add(total, trans);
			}
			else {
				print(cout, total) << endl;
				total = trans;
			}
		}
		print(cout, total) << endl;
	}
	else {
		cerr << "no!" << endl;
	}
	return 0;
}

7.14 编写一个构造函数,令其用我们提供的类内初始值显式地初始化成员。

(把先前版本的接受三参数构造函数屏蔽)

//Sales_data(std::string s,unsigned n,double p):book_no(s),book_sold(n),revenue(n*p){}
	Sales_data(std::string s, unsigned n, double p) 
	{
		book_no = s;
		book_sold = n;
		revenue = p*n;
	}


7.15 为你的Person类添加正确的构造函数。

Person() = default
Person(std::string n):name(n){}
Person(std::string n, std::string a):name(n),adress(a){}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值