forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.15.cpp
44 lines (35 loc) · 937 Bytes
/
7.15.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <string>
#include <iostream>
struct Person {
Person() = default;
Person(const std::string &n) : name(n) {}
Person(const std::string &n, const std::string &a)
: name(n), address(a) {}
Person(std::istream &);
std::string getName() const { return name; }
std::string getAddress() const { return address; }
std::string name;
std::string address;
};
std::istream &read(std::istream &is, Person &rhs) {
is >> rhs.name >> rhs.address;
return is;
}
std::ostream &print(std::ostream &os, const Person &rhs) {
os << rhs.getName() << " " << rhs.getAddress();
return os;
}
Person::Person(std::istream &is) {
read(is, *this);
}
int main() {
Person p1;
Person p2("Zhang San");
Person p3("Zhang San", "Earth");
Person p4(std::cin);
print(std::cout, p1) << std::endl;
print(std::cout, p2) << std::endl;
print(std::cout, p3) << std::endl;
print(std::cout, p4) << std::endl;
return 0;
}