理论讲解请参考:C++ Primer 第五版 第7章类 7.1——类讲解(成员函数、非成员函数、构造函数)
目录
7.1 定义抽象数据类型习题答案
7.4&7.5
应该定义为const类型,这里没有涉及到对对象的修改。考点:是否设置const类型——是否对其修改,无修改就是const类型
#include <iostream>
#include<string>
#include<vector>
using namespace std;
class Person
{
public:
// 类成员,姓名和住址
string name;
string address;
//类成员函数
string getname() const
{
return name;
} //返回姓名
string getaddress() const
{
return address;
} //返回住址
};
int main()
{
Person p;
cout << "Please enter a name: ";
cin >> p.name;
cout << "Please enter an address ";
cin >> p.address;
cout << p.getname()<<"'s address " <<"is "<<p.getaddress()<<endl;
}
7.6&7.7
# include <iostream>
# include <string>
# include <vector>
# include <typeinfo>
using namespace std;
struct Sales_data{
// 类成员,ISBN,卖的数量
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
//类成员函数
string isbn() const { return bookNo; } //返回ISBN号
(Sales_data&) combine(const Sales_data &rhs); //相加
double ave_price() const; //求平均值
};
//Sales_data的非成员接口函数,声明
ostream &print(ostream &, const Sales_data&);
istream &read(istream &, Sales_data&);
//类成员函数,相加两个对象
Sales_data& Sales_data::combine(const Sales_data &rhs){
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
//类成员函数,求平均
double Sales_data::ave_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 = price * item.units_sold;
return is;
}
//非成员函数输出操作
ostream &print(ostream &os, const Sales_data &item){
os << item.isbn() << " " << item.units_sold << " "
<< item.revenue << " " << item.ave_price();
return os;
}
int main()
{
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;
}
7.8
read涉及到对对象的写入,print并么有对其进行修改,只是读取(普通常量引用和常量引用的区别:是否改变)
7.9
#include <iostream>
#include<string>
#include<vector>
using namespace std;
class Person
{
public:
// 类成员,姓名和住址
string name;
string address;
//类成员函数
string getname() const
{
return name;
} //返回姓名
string getaddress() const
{
return address;
} //返回住址
};
//非成员函数读取操作
istream &read(istream &is, Person &item){
is >> item.name >> item.address;
return is;
}
//非成员函数输出操作
ostream &print(ostream &os, const Person &item){
os << item.name<<"'s address is ";
os << item.address;
return os;
}
int main()
{
Person p;
read(cin, p);
print(cout, p);
}
7.10
同时判断两次读取对象的操作
7.15
#include <iostream>
#include<string>
#include<vector>
using namespace std;
class Person
{
public:
Person(const string &Name,const string &Adress) :name(Name),address(Adress){}; //构造函数
Person(); // 默认构造函数
// 类成员,姓名和住址
string name;
string address;
//类成员函数
string getname() const
{
return name;
} //返回姓名
string getaddress() const
{
return address;
} //返回住址
};
//非成员函数读取操作
istream &read(istream &is, Person &item){
is >> item.name >> item.address;
return is;
}
//非成员函数输出操作
ostream &print(ostream &os, const Person &item){
os << item.name<<"'s address is ";
os << item.address;
return os;
}
int main()
{
Person p;
cout << "Please enter a name: ";
cin >> p.name;
cout << "Please enter an address ";
cin >> p.address;
cout << p.getname()<<"'s address " <<"is "<<p.getaddress()<<endl;
}