C++Primer第五版——习题答案详解(六)

时间:2022-06-18 16:06:44

习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html

第7章 类


练习7.1

class Sales_data {
public:
std::string isbn() const { return bookNo; }
Sales_data& combine(const Sales_data&); private:
std::string bookNo;
unsigned units_sold;
double revenue;
}; Sales_data& Sales_data::combine(const Sales_data &rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}

练习7.4

class Person {

private:
std::string name;
std::string address;
};

练习7.5

class Person {
public:
std::string getName() const { return this->name; }
std::string getAddress() const { return this->address; }
private:
std::string name;
std::string address;
};

练习7.6

#include<iostream>
#include<string> using namespace std; class Sales_data {
public:
Sales_data(std::string bN, unsigned sold, double reven) :bookNo(bN), units_sold(sold), revenue(reven) {}
std::string isbn() const { return this->bookNo; }
Sales_data& combine(const Sales_data&);
double avg_price() const;
public:
std::string bookNo;
unsigned units_sold;
double revenue;
}; Sales_data& Sales_data::combine(const Sales_data &rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} double Sales_data::avg_price() const {
if (units_sold) {
return revenue / units_sold;
}
else return 0;
} istream &read(istream &is, Sales_data &item) {
double price = 0;
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.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
return os;
} Sales_data add(const Sales_data &lhs, const Sales_data &rhs) {
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
} int main() {
return 0;
}

练习7.7

#include<iostream>
#include<string> using namespace std; class Sales_data {
public:
Sales_data(){}
Sales_data(std::string bN, unsigned sold, double reven) :bookNo(bN), units_sold(sold), revenue(reven) {}
std::string isbn() const { return this->bookNo; }
Sales_data& combine(const Sales_data&);
double avg_price() const;
public:
std::string bookNo;
unsigned units_sold;
double revenue;
}; Sales_data& Sales_data::combine(const Sales_data &rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} double Sales_data::avg_price() const {
if (units_sold) {
return revenue / units_sold;
}
else return 0;
} istream &read(istream &is, Sales_data &item) {
double price = 0;
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.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
return os;
} Sales_data add(const Sales_data &lhs, const Sales_data &rhs) {
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
} int main() {
Sales_data total;
if (read(cin, total)) {
Sales_data trans; while (read(cin, trans)) {
if (total.isbn() == trans.isbn()) {
total.combine(trans);
}
else {
print(cout, total);
cout << endl;
total = trans;
}
}
print(cout, total);
cout << endl;
return 0;
}
else {
cerr << "No data?" << endl;
return -1;
}
}

练习7.8

read需要改变参数,print不需要改变

练习7.9

#include<iostream>
#include<string>
class Sales_data {
public:
std::string isbn() const { return bookNo; }
Sales_data& combine(const Sales_data&); private:
std::string bookNo;
unsigned units_sold;
double revenue;
}; Sales_data& Sales_data::combine(const Sales_data &rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} class Person {
public:
std::string getName() const { return this->name; }
std::string getAddress() const { return this->address; }
std::ostream &print(std::ostream&, const Person&);
std::istream &read(std::istream&, Person&);
public:
std::string name;
std::string address;
}; std::ostream &print(std::ostream &os, const Person &p) {
os << p.getName() << " " << p.getAddress();
return os;
} std::istream &read(std::istream &is, Person &p) {
is >> p.name >> p.address;
return is;
} int main() {
return 0;
}

练习7.10

是否成功读入data1,data2

练习7.11

#include<iostream>
#include<string> using namespace std; class Sales_data {
public:
Sales_data() = default;
Sales_data(const std::string bN) :bookNo(bN), units_sold(0), revenue(0) {}
Sales_data(const std::string bN, unsigned sold) :bookNo(bN), units_sold(sold), revenue(0) {}
Sales_data(const std::string bN, unsigned sold, double reven) :bookNo(bN), units_sold(sold), revenue(reven) {}
std::string isbn() const { return bookNo; }
unsigned getUnits_sold() const { return units_sold; }
double getRevenue() const{ return revenue; }
Sales_data& combine(const Sales_data&); private:
std::string bookNo;
unsigned units_sold;
double revenue;
}; Sales_data& Sales_data::combine(const Sales_data &rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} int main() {
Sales_data A;
Sales_data B("0-1-33333-2");
Sales_data C("0-1-33333-3", 1);
Sales_data D("0-1-33334-3", 1,50); cout << A.isbn() << ", " << A.getUnits_sold() << ", " << A.getRevenue() << endl;
cout << B.isbn() << ", " << B.getUnits_sold() << ", " << B.getRevenue() << endl;
cout << C.isbn() << ", " << C.getUnits_sold() << ", " << C.getRevenue() << endl;
cout << D.isbn() << ", " << D.getUnits_sold() << ", " << D.getRevenue() << endl; system("pause");
return 0;
}

练习7.12

#include<iostream>
#include<string> using namespace std; class Sales_data; std::istream &read(std::istream &, Sales_data &); class Sales_data {
public:
Sales_data() = default;
Sales_data(const std::string bN) :bookNo(bN), units_sold(0), revenue(0) {}
Sales_data(const std::string bN, unsigned sold) :bookNo(bN), units_sold(sold), revenue(0) {}
Sales_data(const std::string bN, unsigned sold, double price) :bookNo(bN), units_sold(sold), revenue(price * sold) {}
std::string isbn() const { return bookNo; }
unsigned getUnits_sold() const { return units_sold; }
double getRevenue() const { return revenue; }
double avg_price() const;
Sales_data& combine(const Sales_data &);
Sales_data(std::istream &is) { read(is, *this); } public:
std::string bookNo;
unsigned units_sold;
double revenue;
}; Sales_data&::Sales_data::combine(const Sales_data &rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} double Sales_data::avg_price() const {
if (units_sold) {
return revenue / units_sold;
}
else {
return 0;
}
} std::istream &read(std::istream &is, Sales_data &item) {
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
} int main() { return 0;
}

练习7.13

#include<iostream>
#include<string> using namespace std; class Sales_data; std::istream &read(std::istream &, Sales_data &);
std::ostream &print(std::ostream &os, const Sales_data &item);
Sales_data add(const Sales_data &lhs, const Sales_data &rhs); class Sales_data {
public:
Sales_data() = default;
Sales_data(const std::string bN) :bookNo(bN), units_sold(0), revenue(0) {}
Sales_data(const std::string bN, unsigned sold) :bookNo(bN), units_sold(sold), revenue(0) {}
Sales_data(const std::string bN, unsigned sold, double price) :bookNo(bN), units_sold(sold), revenue(price * sold) {}
std::string isbn() const { return bookNo; }
unsigned getUnits_sold() const { return units_sold; }
double getRevenue() const { return revenue; }
double avg_price() const;
Sales_data& combine(const Sales_data &);
Sales_data(std::istream &is) { read(is, *this); } public:
std::string bookNo;
unsigned units_sold;
double revenue;
}; Sales_data&::Sales_data::combine(const Sales_data &rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} double Sales_data::avg_price() const {
if (units_sold) {
return revenue / units_sold;
}
else {
return 0;
}
} std::istream &read(std::istream &is, Sales_data &item) {
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
} std::ostream &print(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price(); return os;
} Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs); return sum;
} int main() {
Sales_data sales_data1;
print(std::cout, sales_data1) << endl; Sales_data sales_data2("1-01");
print(std::cout, sales_data2) << std::endl; Sales_data sales_data3("1-01", 1, 100);
print(std::cout, sales_data3) << std::endl; Sales_data sales_data4(std::cin);
print(std::cout, sales_data4) << std::endl; system("pause");
return 0;
}

练习7.14

Sales_data() : bookNo(""), units_sold(0) , revenue(0){ }

练习7.15

class Person {
public:
Person() = default;
Person(const std::string nm, const std::string ad) :name(nm), address(ad) {}
std::string getName() const { return this->name; }
std::string getAddress() const { return this->address; }
std::ostream &print(std::ostream&, const Person&);
std::ostream &read(std::istream&, Person&);
public:
std::string name;
std::string address;
};

练习7.16

访问位置有限定,次数没有限定。

public后的成员可以被整个程序内访问,而private只能被类的成员函数访问。

练习7.17

有,struct内的变量访问权限是public,而class的访问权限可以是private、public、protected

练习7.18

封装,即隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别。

练习7.21

#include<iostream>
#include<string> using namespace std; class Sales_data {
public:
friend std::istream &read(std::istream &, Sales_data &);
friend std::ostream &print(std::ostream &os, const Sales_data &item);
friend Sales_data add(const Sales_data &lhs, const Sales_data &rhs); Sales_data() = default;
Sales_data(const std::string bN) :bookNo(bN), units_sold(0), revenue(0) {}
Sales_data(const std::string bN, unsigned sold) :bookNo(bN), units_sold(sold), revenue(0) {}
Sales_data(const std::string bN, unsigned sold, double price) :bookNo(bN), units_sold(sold), revenue(price * sold) {}
std::string isbn() const { return bookNo; }
unsigned getUnits_sold() const { return units_sold; }
double getRevenue() const { return revenue; }
double avg_price() const;
Sales_data& combine(const Sales_data &);
Sales_data(std::istream &is) { read(is, *this); } private:
std::string bookNo;
unsigned units_sold;
double revenue;
}; Sales_data&::Sales_data::combine(const Sales_data &rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} double Sales_data::avg_price() const {
return units_sold > 0 ? revenue / units_sold : 0;
} std::istream &read(std::istream &is, Sales_data &item) {
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
} std::ostream &print(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price(); return os;
} Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs); return sum;
} int main() {
Sales_data sales_data1;
print(std::cout, sales_data1) << endl; Sales_data sales_data2("1-01");
print(std::cout, sales_data2) << std::endl; Sales_data sales_data3("1-01", 1, 100);
print(std::cout, sales_data3) << std::endl; Sales_data sales_data4(std::cin);
print(std::cout, sales_data4) << std::endl; system("pause");
return 0;
}

练习7.22

class Person {
public:
Person() = default;
Person(const std::string nm, const std::string ad) :name(nm), address(ad) {}
std::string getName() const { return this->name; }
std::string getAddress() const { return this->address; }
friend std::ostream &print(std::ostream&, const Person&);
friend std::istream &read(std::istream&, Person&);
private:
std::string name;
std::string address;
};

练习7.23-7.24

class Screen {
public:
typedef std::string::size_type pos;
Screen() = default;
Screen(pos ht, pos wd) :height(ht), width(wd) {}
Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c) {} char get() const { return contents[cursor]; }
char get(pos r, pos c) const { return contents[r * width + c]; } private:
mutable size_t access_ctr;
pos height, width, cursor;
std::string contents;
};

练习7.25

可以,因为Screen类中只有string类和内置类型,他们都可以使用拷贝和赋值操作。

练习7.26

#include<iostream>
#include<string> using namespace std; class Sales_data {
public:
friend std::istream &read(std::istream &, Sales_data &);
friend std::ostream &print(std::ostream &os, const Sales_data &item);
friend Sales_data add(const Sales_data &lhs, const Sales_data &rhs); Sales_data() = default;
Sales_data(const std::string bN) :bookNo(bN), units_sold(0), revenue(0) {}
Sales_data(const std::string bN, unsigned sold) :bookNo(bN), units_sold(sold), revenue(0) {}
Sales_data(const std::string bN, unsigned sold, double price) :bookNo(bN), units_sold(sold), revenue(price * sold) {}
std::string isbn() const { return bookNo; }
unsigned getUnits_sold() const { return units_sold; }
double getRevenue() const { return revenue; }
inline double avg_price() const;
Sales_data& combine(const Sales_data &);
Sales_data(std::istream &is) { read(is, *this); } private:
std::string bookNo;
unsigned units_sold;
double revenue;
}; Sales_data&::Sales_data::combine(const Sales_data &rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} inline double Sales_data::avg_price() const {
return units_sold > 0 ? revenue / units_sold : 0;
} std::istream &read(std::istream &is, Sales_data &item) {
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
} std::ostream &print(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price(); return os;
} Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs); return sum;
} int main() {
Sales_data sales_data1;
print(std::cout, sales_data1) << endl; Sales_data sales_data2("1-01");
print(std::cout, sales_data2) << std::endl; Sales_data sales_data3("1-01", 1, 100);
print(std::cout, sales_data3) << std::endl; Sales_data sales_data4(std::cin);
print(std::cout, sales_data4) << std::endl; system("pause");
return 0;
}

练习7.27

#include<iostream>
#include<string> using namespace std; class Screen {
public:
typedef std::string::size_type pos;
Screen() = default;
Screen(pos ht, pos wd) :height(ht), width(wd) {}
Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c) {} Screen &set(char);
Screen &set(pos, pos, char);
Screen &move(pos, pos); char get() const { return contents[cursor]; }
char get(pos r, pos c) const { return contents[r * width + c]; } Screen &display(std::ostream &os)
{
do_display(os);
return *this;
} const Screen &display(std::ostream &os) const
{
do_display(os);
return *this;
} void do_display(std::ostream &os) const { os << contents; } private:
mutable size_t access_ctr;
pos height, width, cursor;
std::string contents;
}; inline Screen &Screen::set(char c) {
contents[cursor] = c;
return *this;
} inline Screen &Screen::set(pos r, pos c, char ch) {
contents[r*width + c] = ch;
return *this;
} inline Screen &Screen::move(pos r, pos c) {
cursor = r * width + c;
return *this;
} int main() {
Screen myScreen(5, 5, 'X');
myScreen.display(cout);
cout << endl;
myScreen.move(4, 0).set('#').display(cout);
cout << endl; myScreen.display(cout);
cout << endl; system("pause");
return 0;
}

练习7.28-7.29

如果是Screen则不会修改字符。

练习7.31

class Y;

class X {
Y *y = nullptr;
}; class Y {
X x;
};

练习7.32

class Screen;

class Window_mgr {
public:
using ScreenIndex = std::vector<Screen>::size_type;
void clear(ScreenIndex);
private:
std::vector<Screen> screens;
}; class Screen { friend void Window_mgr::clear(ScreenIndex); public:
typedef std::string::size_type pos;
Screen() = default;
Screen(pos ht, pos wd) :height(ht), width(wd) {}
Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c) {} Screen &set(char);
Screen &set(pos, pos, char);
Screen &move(pos, pos); char get() const { return contents[cursor]; }
char get(pos r, pos c) const { return contents[r * width + c]; } Screen &display(std::ostream &os)
{
do_display(os);
return *this;
} const Screen &display(std::ostream &os) const
{
do_display(os);
return *this;
} void do_display(std::ostream &os) const { os << contents; } private:
mutable size_t access_ctr;
pos height, width, cursor;
std::string contents;
};

练习7.34

报错,pos未声明

练习7.35

typedef string Type;
Type initVal(); // string
class Exercise {
public:
typedef double Type;
Type setVal(Type); // double
Type initVal(); // double
private:
int val;
}; Type Exercise::setVal(Type parm) { // first is `string`, second is `double`
val = parm + initVal();
return val;
}

练习7.36

成员的初始化顺序与他们在类定义中的出现顺序一致,所以先初始化rem再是base,所以结果不正确。

将类内定义顺序改成:

int base, rem;

练习7.37

Sales_data first_item(cin);   // Sales_data(std::istream &is) ;

int main() {
Sales_data next; // Sales_data(std::string s = ""): bookNo = "", cnt = 0, revenue = 0.0
Sales_data last("9-999-99999-9"); // Sales_data(std::string s = ""): bookNo = "9-999-99999-9", cnt = 0, revenue = 0.0
}

练习7.38

Sales_data(std::istream &is = std::cin) { read(is, *this); }

练习7.41

#include <string>
#include <iostream> using namespace std; struct Sales_data; std::istream &read(std::istream &is, Sales_data &item);
std::ostream &print(std::ostream &os, const Sales_data &item);
Sales_data add(const Sales_data &lhs, const Sales_data &rhs); struct Sales_data
{
friend std::istream &read(std::istream &is, Sales_data &item);
friend std::ostream &print(std::ostream &os, const Sales_data &item);
friend Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
public:
Sales_data(const std::string &s, unsigned n, double p) : bookNo(s), units_sold(n), revenue(p*n)
{
std::cout << "Sales_data(const std::string &s, unsigned n, double p)" << std::endl;
}
Sales_data() : Sales_data("", 0, 0)
{
std::cout << "Sales_data() : Sales_data(\"\", 0, 0)" << std::endl;
}
Sales_data(const std::string &s) : Sales_data(s, 0, 0)
{
std::cout << "Sales_data(const std::string &s) : Sales_data" << std::endl;
}
Sales_data(std::istream &is) : Sales_data()
{
read(is, *this);
std::cout << "Sales_data(std::istream &is) : Sales_data()" << std::endl;
}
std::string isbn() const { return bookNo; }
Sales_data& combine(const Sales_data&);
private:
inline double avg_price() const; std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
}; Sales_data& Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue; return *this;
} inline double Sales_data::avg_price() const
{
if (units_sold)
return revenue / units_sold;
else
return 0;
} std::istream &read(std::istream &is, Sales_data &item)
{
double price = 0; is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold; return is;
} std::ostream &print(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price(); return os;
} Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs); return sum;
} int main() {
Sales_data a("0-1-999-9", 2, 10);
cout << endl; Sales_data b;
cout << endl; Sales_data c("0-1-999-9");
cout << endl; Sales_data d(cin); system("pause");
return 0;
}

C++Primer第五版——习题答案详解(六)

练习7.43

class NoDefault {
public:
NoDefault(int i) {}
}; class C {
public:
C() :no(0) {}
private:
NoDefault no;
};

练习7.44

非法,NoDefault没有定义默认构造函数

练习7.45

合法,C有默认构造函数

练习7.48

都没问题,因为都是显式构造了Sales_data对象

练习7.50

explicit Person(std::istream &is) { read(is, *this); }

练习7.52

该初始化是用花括号括起来的成员初始值列表,需要定义聚合类。

struct Sales_data {
std::string isbn;
unsigned units_sold;
double revenue;
};

练习7.53

class Debug
{
public:
constexpr Debug(bool b = true) : hw(b), io(b), other(b) {}
constexpr Debug(bool h, bool i, bool o) : hw(h), io(i), other(o) {} constexpr bool amy() { return hw || io || other; }
void set_io(bool b) { io = b; }
void set_hw(bool b) { hw = b; }
void set_other(bool b) { other = b; }
private:
bool hw;
bool io;
bool other;
};

练习7.56

类的静态成员与类本身直接相关,而不是与类的各个对象保持关联。 

每个对象不需要存储公共数据,如果数据被改变,则每个对象都可以使用新值。

静态数据成员可以是不完全类型; 可以使用静态成员作为默认实参。

练习7.57

#include<iostream>
#include<string>
using namespace std; //全局变量 利率
//double interestRate; class Account {
public:
Account(std::string name, double money) :owner(name), amount(money) {}
double getAmount() const {
return this->amount;
}
void deposit(double money) {
this->amount += money;
}
double rate() { return interestRate; }
static void rate(double newRate){ //静态的成员函数不属于任何一个对象,所以this不能用,this指的是当前对象。
interestRate = newRate;
}
void applyint() { amount += amount * interestRate; } private:
std::string owner;
double amount;
static double interestRate; //只有一个,不能在这初始化
static const int period = 30; //例外
}; double Account::interestRate = 0.015; int main() {
//interestRate = 0.015; Account::rate(0.026); //函数加static以后可以用这种方法修改static变量 Account a("张三", 1000);
Account b("李四", 2000); cout << a.rate() << endl; a.deposit(500);
b.deposit(600); cout << a.getAmount() << endl;
cout << b.getAmount() << endl; cout << a.rate() << endl;
a.rate(0.08);
cout << a.rate() << endl;
cout << b.rate() << endl; Account::rate(0.02);
a.applyint();
b.applyint();
cout << a.getAmount() << endl;
cout << b.getAmount() << endl; system("pause");
return 0;
}

练习7.58

// example.h
class Example {
public:
static double rate; //不能直接初始化
static const int vecSize = 20;
static vector<double> vec;
};
// example.C
#include "example.h"
double Example::rate = 6.5;
vector<double> Example::vec(Example::vecSize);