本文主要是介绍C++ Primer (第五版)第七章习题部分答案(上),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在我自学C++过程中,我选择了C++Primer这本书,并对部分代码习题进行了求解以及运行结果。接下来几个月我将为大家定时按章节更新习题答案与运行结果,运行环境(Visual Studio Code,windows 11):
C++ Primer (第五版)习题答案总目录:
C++ Primer (第五版)习题答案总目录-CSDN博客
7.1 使用2.6.1节定义的Sales_data类为1.6节的交易处理程序编写一个新版本。
#include <iostream>
#include <string>
using namespace std;
struct Sale_data
{string bookNo;unsigned units_sold = 0;double revenue = 0.0;
};
int main()
{Sale_data total;if (cin >> total.bookNo >> total.units_sold >> total.revenue){Sale_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成员。
#pragma once
#ifndef SALESDATA_7_2_H_
#define SALESDATA_7_2_H_#include <string>
#include <iostream>using namespace std;struct Salesdata
{string bookNo;unsigned units_sold = 0;double revenue = 0;string isbn() const{return bookNo;}Salesdata& combine(const Salesdata&);
};Salesdata& Salesdata::combine(const Salesdata& rhs)
{units_sold += rhs.units_sold;revenue += rhs.revenue;return *this;
}
#endif // !SALESDATA_7_2_H_
7.3 修改7.1.1节的交易处理程序,令其使用这些成员。
.h
#pragma once
#ifndef SALESDATA_7_2_H_
#define SALESDATA_7_2_H_#include <string>
#include <iostream>using namespace std;struct Salesdata
{string bookNo;unsigned units_sold = 0;double revenue = 0;string isbn() const{return bookNo;}Salesdata& combine(const Salesdata&);
};Salesdata& Salesdata::combine(const Salesdata& rhs)
{units_sold += rhs.units_sold;revenue += rhs.revenue;return *this;
}
#endif // !SALESDATA_7_2_H_
.cpp
#include <iostream>
#include <string>
#include "SalesData_7_2.h"
using namespace std;int main()
{Salesdata total;if (cin >> total.bookNo >> total.units_sold >> total.revenue){Salesdata trans;while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue){if (total.bookNo == trans.bookNo){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对象存放这些元素,接下来的练习将不断充实这个类的其他特征。
#pragma once
#ifndef PERSON_H_
#define PERSON_H_#include <string>
#include <iostream>
using namespace std;
struct Person
{string name;string address;
};
#endif // !PERSON_H_
7.5 在你的Person类中提供一些操作使其能够返回姓名和地址。这些函数是否应该是const的呢?解释原因。
#pragma once
#ifndef PERSON_H_
#define PERSON_H_#include <string>
#include <iostream>
using namespace std;
struct Person
{string name;string address;string getname() const{return name;}string getaddress() const{return address;}
};
#endif // !PERSON_H_
应该是const,这两个成员函数只需读取成员对象,无需改变成员对象。
7.6 对于函数add、read和print,定义你自己的版本。
#pragma once
#ifndef SALESDATA_7_2_H_
#define SALESDATA_7_2_H_#include <string>
#include <iostream>using namespace std;struct Salesdata
{string bookNo;unsigned units_sold = 0;double revenue = 0;string isbn() const{return bookNo;}Salesdata& combine(const Salesdata&);
};Salesdata& Salesdata::combine(const Salesdata& rhs)
{units_sold += rhs.units_sold;revenue += rhs.revenue;return *this;
}
istream& read(istream& is, Salesdata& item)
{double price = 0;is >> item.bookNo >> item.units_sold >> price;item.revenue = item.units_sold * price;return is;
}
ostream& print(ostream& os, const Salesdata& item) //因为IO类属于不能被拷贝的类型,因此只能通过引用来传递它们
{os << item.isbn() << " " << item.units_sold << " " << item.revenue;return os;
}Salesdata add(const Salesdata& item1, const Salesdata& item2)
{Salesdata sum = item1;sum.combine(item2);return sum;
}
#endif // !SALESDATA_7_2_H_
7.7 使用这些新函数重写7.1.2节练习中的程序。
.h
#pragma once
#ifndef SALESDATA_7_2_H_
#define SALESDATA_7_2_H_#include <string>
#include <iostream>using namespace std;struct Salesdata
{string bookNo;unsigned units_sold = 0;double revenue = 0;string isbn() const{return bookNo;}Salesdata& combine(const Salesdata&);
};Salesdata& Salesdata::combine(const Salesdata& rhs)
{units_sold += rhs.units_sold;revenue += rhs.revenue;return *this;
}
istream& read(istream& is, Salesdata& item)
{double price = 0;is >> item.bookNo >> item.units_sold >> price;item.revenue = item.units_sold * price;return is;
}
ostream& print(ostream& os, const Salesdata& item) //因为IO类属于不能被拷贝的类型,因此只能通过引用来传递它们
{os << item.isbn() << " " << item.units_sold << " " << item.revenue;return os;
}Salesdata add(const Salesdata& item1, const Salesdata& item2)
{Salesdata sum = item1;sum.combine(item2);return sum;
}
#endif // !SALESDATA_7_2_H_
.cpp
#include <iostream>
#include <string>
#include "SalesData_7_2.h"
using namespace std;int main()
{Salesdata total;if (read(cin,total)){Salesdata 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{std::cerr << "No data?!" << std::endl;return -1;}return 0;
}
7.9 对于7.1.2节练习中代码,添加读取和打印Person对象的操作。
#pragma once
#ifndef PERSON_H_
#define PERSON_H_#include <string>
#include <iostream>
using namespace std;
struct Person
{string name;string address;string getname() const{return name;}string getaddress() const{return address;}
};
istream& read(istream& is, Person& item)
{double price = 0;is >> item.name >> item.address ;return is;
}
ostream& print(ostream& os, const Person& item) //因为IO类属于不能被拷贝的类型,因此只能通过引用来传递它们
{os << item.name << " " << item.address << endl;return os;
}
#endif // !PERSON_H_
7.11 在你的Sales_data类中添加构造函数,然后编写一段程序令其用到每个构造函数。
.h
#pragma once
#ifndef SALESDATA_7_2_H_
#define SALESDATA_7_2_H_#include <string>
#include <iostream>using namespace std;struct Salesdata
{string bookNo;unsigned units_sold = 0;double revenue = 0;string isbn() const{return bookNo;}Salesdata& combine(const Salesdata&);Salesdata() = default; //此种情况,指令编译器执行合成的默认构造函数来初始化Salesdata(const string& s) : bookNo(s) { }; //构造函数中未给定初始值的部分,利用数据成员初始值进行初始化Salesdata(const string& s, unsigned n, double p) : bookNo(s), units_sold(n), revenue(n* p) { };Salesdata(istream& is);
};Salesdata& Salesdata::combine(const Salesdata& rhs)
{units_sold += rhs.units_sold;revenue += rhs.revenue;return *this;
}
inline Salesdata::Salesdata(istream& is)
{
}
istream& read(istream& is, Salesdata& item)
{double price = 0;is >> item.bookNo >> item.units_sold >> price;item.revenue = item.units_sold * price;return is;
}
ostream& print(ostream& os, const Salesdata& item) //因为IO类属于不能被拷贝的类型,因此只能通过引用来传递它们
{os << item.isbn() << " " << item.units_sold << " " << item.revenue<<endl;return os;
}Salesdata add(const Salesdata& item1, const Salesdata& item2)
{Salesdata sum = item1;sum.combine(item2);return sum;
}
#endif // !SALESDATA_7_2_H_
.cpp
#include <iostream>
#include <string>
#include "SalesData_7_2.h"
using namespace std;int main()
{Salesdata t1;print(cout, t1);Salesdata t2("Joey!");print(cout, t1);Salesdata t3("Andy",5,100);print(cout, t3);Salesdata t4(cin);print(cout, t4);return 0;
}
7.12 把只接受一个istream 作为参数的构造函数移到类的内部。
#pragma once
#ifndef SALESDATA_7_2_H_
#define SALESDATA_7_2_H_#include <string>
#include <iostream>using namespace std;
struct Salesdata;
istream& read(istream& is, Salesdata& item);
struct Salesdata
{string bookNo;unsigned units_sold = 0;double revenue = 0;string isbn() const{return bookNo;}Salesdata& combine(const Salesdata&);Salesdata() = default; //此种情况,指令编译器执行合成的默认构造函数来初始化Salesdata(const string& s) : bookNo(s) { }; //构造函数中未给定初始值的部分,利用数据成员初始值进行初始化Salesdata(const string& s, unsigned n, double p) : bookNo(s), units_sold(n), revenue(n* p) { };Salesdata(istream& is){read(is, *this);}
};Salesdata& Salesdata::combine(const Salesdata& rhs)
{units_sold += rhs.units_sold;revenue += rhs.revenue;return *this;
}
inline Salesdata::Salesdata(istream& is)
{
}
istream& read(istream& is, Salesdata& item)
{double price = 0;is >> item.bookNo >> item.units_sold >> price;item.revenue = item.units_sold * price;return is;
}
ostream& print(ostream& os, const Salesdata& item) //因为IO类属于不能被拷贝的类型,因此只能通过引用来传递它们
{os << item.isbn() << " " << item.units_sold << " " << item.revenue<<endl;return os;
}Salesdata add(const Salesdata& item1, const Salesdata& item2)
{Salesdata sum = item1;sum.combine(item2);return sum;
}
#endif // !SALESDATA_7_2_H_
7.13 使用istream构造函数重写第229页的程序。
.h
#pragma once
#ifndef SALESDATA_7_2_H_
#define SALESDATA_7_2_H_#include <string>
#include <iostream>using namespace std;
struct Salesdata;
istream& read(istream& is, Salesdata& item);
struct Salesdata
{string bookNo;unsigned units_sold = 0;double revenue = 0;string isbn() const{return bookNo;}Salesdata& combine(const Salesdata&);Salesdata() = default; //此种情况,指令编译器执行合成的默认构造函数来初始化Salesdata(const string& s) : bookNo(s) { }; //构造函数中未给定初始值的部分,利用数据成员初始值进行初始化Salesdata(const string& s, unsigned n, double p) : bookNo(s), units_sold(n), revenue(n* p) { };Salesdata(istream& is){read(is, *this);}
};Salesdata& Salesdata::combine(const Salesdata& rhs)
{units_sold += rhs.units_sold;revenue += rhs.revenue;return *this;
}
inline Salesdata::Salesdata(istream& is)
{
}
istream& read(istream& is, Salesdata& item)
{double price = 0;is >> item.bookNo >> item.units_sold >> price;item.revenue = item.units_sold * price;return is;
}
ostream& print(ostream& os, const Salesdata& item) //因为IO类属于不能被拷贝的类型,因此只能通过引用来传递它们
{os << item.isbn() << " " << item.units_sold << " " << item.revenue<<endl;return os;
}Salesdata add(const Salesdata& item1, const Salesdata& item2)
{Salesdata sum = item1;sum.combine(item2);return sum;
}
#endif // !SALESDATA_7_2_H_
.cpp
#include <iostream>
#include <string>
#include "SalesData_7_2.h"
using namespace std;int main()
{Salesdata total(cin);if (!total.isbn().empty()){while(cin){Salesdata trans(cin);if (total.bookNo == trans.bookNo){total.combine(trans);}else{print(cout, total);total = trans;}}}else{std::cerr << "No data?!" << std::endl;return -1;}return 0;
}
7.14 编写一个构造函数,令其用我们提供的类内初始值显式地初始化成员。
Sales_data() : units_sold(0) , revenue(0) { }
7.15 为你的 Person 类添加正确的构造函数。
#pragma once
#ifndef PERSON_H_
#define PERSON_H_#include <string>
#include <iostream>
using namespace std;
struct Person;
istream& read(istream& is, Person& item);struct Person
{string name;string address;string getname() const{return name;}string getaddress() const{return address;}Person() = default;Person(istream& is) { read(is, *this); };Person(string& NM) : name(NM) { };Person(string& NM, string& ADD) : name(NM), address(ADD) { }
};
istream& read(istream& is, Person& item)
{double price = 0;is >> item.name >> item.address ;return is;
}
ostream& print(ostream& os, const Person& item) //因为IO类属于不能被拷贝的类型,因此只能通过引用来传递它们
{os << item.name << " " << item.address << endl;return os;
}
#endif // !PERSON_H_
7.19 在你的Person 类中,你将把哪些成员声明成public 的?哪些声明成private 的?解释你这样做的原因。
构造函数、getName()、getAddress() 函数将设为 public。name 和 address 将设为private。
函数是暴露给外部的接口,因此要设为public;而数据则应该隐藏让外部不可见。
#pragma once
#ifndef PERSON_H_
#define PERSON_H_#include <string>
#include <iostream>
using namespace std;
struct Person;
istream& read(istream& is, Person& item);struct Person
{
private:string name;string address;
public:string getname() const{return name;}string getaddress() const{return address;}Person() = default;Person(istream& is) { read(is, *this); };Person(string& NM) : name(NM) { };Person(string& NM, string& ADD) : name(NM), address(ADD) { }
};
//istream& read(istream& is, Person& item)
//{
// double price = 0;
// is >> item.name >> item.address ;
// return is;
//}
//ostream& print(ostream& os, const Person& item) //因为IO类属于不能被拷贝的类型,因此只能通过引用来传递它们
//{
// os << item.name << " " << item.address << endl;
// return os;
//}
#endif // !PERSON_H_
7.21 修改你的Sales_data 类使其隐藏实现的细节。你之前编写的关于Sales_data操作的程序应该继续使用,借助类的新定义重新编译该程序,确保其正常工作。
.h
#pragma once
#ifndef SALESDATA_7_2_H_
#define SALESDATA_7_2_H_#include <string>
#include <iostream>using namespace std;
struct Salesdata;
istream& read(istream& is, Salesdata& item);
Salesdata add(const Salesdata& item1, const Salesdata& item2);
ostream& print(ostream& os, const Salesdata& item);
struct Salesdata
{friend istream& read(istream& is, Salesdata& item);friend Salesdata add(const Salesdata& item1, const Salesdata& item2);friend ostream& print(ostream& os, const Salesdata& item);
public:string isbn() const{return bookNo;}Salesdata& combine(const Salesdata&);Salesdata() = default; //此种情况,指令编译器执行合成的默认构造函数来初始化Salesdata(const string& s) : bookNo(s) { }; //构造函数中未给定初始值的部分,利用数据成员初始值进行初始化Salesdata(const string& s, unsigned n, double p) : bookNo(s), units_sold(n), revenue(n* p) { };Salesdata(istream& is){read(is, *this);}
private:string bookNo;unsigned units_sold = 0;double revenue = 0;
};Salesdata& Salesdata::combine(const Salesdata& rhs)
{units_sold += rhs.units_sold;revenue += rhs.revenue;return *this;
}istream& read(istream& is, Salesdata& item)
{double price = 0;is >> item.bookNo >> item.units_sold >> price;item.revenue = item.units_sold * price;return is;
}
ostream& print(ostream& os, const Salesdata& item) //因为IO类属于不能被拷贝的类型,因此只能通过引用来传递它们
{os << item.isbn() << " " << item.units_sold << " " << item.revenue<<endl;return os;
}Salesdata add(const Salesdata& item1, const Salesdata& item2)
{Salesdata sum = item1;sum.combine(item2);return sum;
}
#endif // !SALESDATA_7_2_H_
.cpp
#include <iostream>
#include <string>
#include "SalesData_7_2.h"
using namespace std;int main()
{Salesdata total(cin);if (!total.isbn().empty()){while(cin){Salesdata trans(cin);if (total.isbn() == trans.isbn()){total.combine(trans);}else{print(cout, total);total = trans;}}}else{std::cerr << "No data?!" << std::endl;return -1;}return 0;
}
7.22 修改你的Person 类使其隐藏实现的细节。
#pragma once
#ifndef PERSON_H_
#define PERSON_H_#include <string>
#include <iostream>
using namespace std;
struct Person;
istream& read(istream& is, Person& item);struct Person
{friend istream& read(istream& is, Person& item);friend ostream& print(ostream& os, const Person& item);
private:string name;string address;
public:string getname() const{return name;}string getaddress() const{return address;}Person() = default;Person(istream& is) { read(is, *this); };Person(string& NM) : name(NM) { };Person(string& NM, string& ADD) : name(NM), address(ADD) { }
};
istream& read(istream& is, Person& item)
{double price = 0;is >> item.name >> item.address ;return is;
}
ostream& print(ostream& os, const Person& item) //因为IO类属于不能被拷贝的类型,因此只能通过引用来传递它们
{os << item.name << " " << item.address << endl;return os;
}
#endif // !PERSON_H_
这篇关于C++ Primer (第五版)第七章习题部分答案(上)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!