当前位置:   article > 正文

《C++ Primer》5th 课后练习 第七章 类 1~10_在你的person类中提供一些操作

在你的person类中提供一些操作

练习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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

练习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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

练习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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

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

class Person {
	string name;
	string addr;
};
  • 1
  • 2
  • 3
  • 4

练习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; }
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

应该是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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
//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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

练习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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

练习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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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

read 函数的返回值是 istream 对象,if语句中条件部分的作用是从输入流中读取数据给两个data对象。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/136502
推荐阅读
相关标签
  

闽ICP备14008679号