C++Prime 练习题7.4解答
包含了一些对类外构造函数、输入输出流、函数形参中的引用类型、返回值类型为引用类型的思考
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
Person() = default;
Person(istream& is);
string getName() const {
return this->name;
}
string getAddress() const {
return this->address;
}
ostream& print(ostream& os, const Person& person) {
os << person.name << endl << person.address << endl;
return os;
}
istream& read(istream& is, Person& person) {
is >> person.name >> person.address;
return is;
}
private:
string name;
string address;
};
Person::Person(istream& is) {
read(is, *this);
}
void test01() {
Person p1(cin);
p1.print(cout, p1);
}
int main() {
test01();
system("pause");
return 0;
}