本文为C++ primer第5版第七章的练习题记录,因为是初学者,觉得这一章比较难,代码质量应该不高..........
7.1.1节练习
练习7.1
#include <iostream>
#include <string>
using namespace std;
struct Sales_data
{
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main()
{
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 = trans;
}
}
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}
7.1.2节练习
练习7.2
struct Sales_data
{
string isbn() const { return bookNo; };
Sales_data& combine(const Sales_data&);
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;
}
练习7.3
struct Sales_data
{
string isbn() const { return bookNo; };
Sales_data& combine(const Sales_data&);
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;
}
int main()
{
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.bookNo << " " << total.units_sold << " " << total.revenue << endl;
total = trans;
}
}
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
}
else
{
cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}
练习7.4
struct Person
{
string Name;
string Address;
};
练习7.5
struct Person
{
string isname() const { return Name; }
string isAddress() const { return Address; }
string Name;
string Address;
};
应该是const,因为该函数只调用而并不改变调用对象的内容。
7.1.3节练习
练习7.6
略
练习7.7
struct Sales_data
{
string isbn() const { return bookNo; };
Sales_data& combine(const Sales_data&);
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;
}
istream& read(istream& is, Sales_data& item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> item.revenue;
return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue ;
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.bookNo == trans.bookNo)
{
total = add(total,trans);
}
else
{
print(cout,total);
cout << endl;
total = trans;
}
}
print(cout, total);
cout << endl;
}
else
{
cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}
练习7.8
因为读取和写入的操作会改变流的内容,所以 read 函数接受的是普通引用。
打印不会改变流的内容,所以 print 函数将其参数定义成常量引用。
练习7.9
struct Person
{
string isname() const { return Name; }
string isAddress() const { return Address; }
string Name;
string Address;
};
istream& read(istream& is, Person& human)
{
is >> human.Name >> human.Address;
return is;
}
ostream& print(ostream& os, const Person& human)
{
os << human.Name << " " << human.Address;
return os;
}
练习7.10
if 检验是否 data1 和 data2 的数据都读入了,都读入了则条件满足,反之不满足。
7.1.4节练习
练习7.11
struct Sales_data
{
/* 构造函数*/
Sales_data() = default;
Sales_data(const string &s) : bookNo(s) { }
Sales_data(const string &s ,unsigned n, double p) : bookNo(s), units_sold(n), revenue(p*n) { }
Sales_data(istream &is);
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main()
{
Sales_data data1;
Sales_data data2("123456789");
Sales_data data3("123456789, 5 , 200");
Sales_data data4(cin);
cout << data1; // 最后一句报错,没有与这些操作数匹配的>>运算符,还不知道为什么,我好菜,等我学会了再回来改好了
}
练习7.12
struct Sales_data
{
/* 构造函数*/
Sales_data() = default;
Sales_data(const string &s) : bookNo(s) { }
Sales_data(const string &s ,unsigned n, double p) : bookNo(s), units_sold(n), revenue(p*n) { }
Sales_data(istream &is);
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main()
{
Sales_data data1;
Sales_data data2("123456789");
Sales_data data3("123456789, 5 , 200");
Sales_data data4(cin);
cout << data1;
}
练习7.13
int main()
{
Sales_data total(cin);
if (cin)
{
Sales_data trans;
while (read(cin,trans))
{
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
{
cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}
练习7.14
Sales_data(const string &s):
bookNo(s), units_sold(0), revenue(0) {}
练习7.15
struct Person
{
/* 构造函数 */
Person(const string& s) :
Name(s), Address(s) { }
string isname() const { return Name; }
string isAddress() const { return Address; }
string Name;
string Address;
};
istream& read(istream& is, Person& human)
{
is >> human.Name >> human.Address;
return is;
}
ostream& print(ostream& os, const Person& human)
{
os << human.Name << " " << human.Address;
return os;
}
7.2节练习
练习7.16
在类的定义中对于访问说明符出现的位置没有严格要求。作为接口的一部分,构造函数和部分成员函数紧跟在 public 说明符之后,而数据成员和作为实现部分的函数则跟在 private 说明符后面。
一个类可以包含0个或多个访问说明符,而且对于某个访问说明符能出现多少次也没有严格规定。
练习7.17
使用class 和 struct 定义类的唯一区别就是默认的访问权限不一样。
练习7.18
封装就是分离类的实现与接口,从而隐藏类的实现细节。在C++语言中,通过把实现部分设为private 完成封装的任务。
练习7.19
在我的Person类中,将构造函数声明为public,Name 和 Address 两个数据成员声明为 private。
?7.2.1节练习
练习7.20
需要类允许其他类或者函数访问它的非公有成员,可以使用友元。
利:使非成员函数访问类的私有成员;
弊:若使用不慎,可能破坏类的封装性。
练习7.21
struct Sales_data
{
public:
/* 构造函数*/
Sales_data() = default;
Sales_data(const string& s) : bookNo(s) { }
Sales_data(const string& s, unsigned n, double p) : bookNo(s), units_sold(n), revenue(p* n) { }
Sales_data(istream& is);
private:
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
练习7.22
struct Person
{
public:
/* 构造函数 */
Person(const string& s) :
Name(s), Address(s) { }
string isname() const { return Name; }
string isAddress() const { return Address; }
private:
string Name;
string Address;
};
7.3.1节练习
练习7.23
class Screen
{
private:
int cursor = 0;
double height = 0, width = 0;
string contents;
};
练习7.24
class Screen
{
public:
Screen() = default;
Screen(double ht, double wd) : height(ht),width(wd),contents(ht * wd,' ') { }
Screen(double ht, double wd, char c : height(ht), width(wd), contents(ht* wd,c) { }
private:
int cursor = 0;
double height = 0, width = 0;
string contents;
};
练习7.25
可以,因为Screen的4个数据成员都是内置类型。
练习7.26
class Screen
{
public:
double avg_price() const;
};
inline double Sales_data::avg_price() const
{
if (units_sold)
return revenue / units_sold;
else
return 0;
}
|