forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.14.cpp
38 lines (34 loc) · 756 Bytes
/
15.14.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
#include <string>
#include <iostream>
using std::string;
using std::ostream;
class base {
public:
string name() { return basename; }
virtual void print(ostream &os) { os << basename; }
private:
string basename;
};
class derived : public base {
public:
void print(ostream &os) { base::print(os); os << " " << i; }
private:
int i = 0;
};
int main() {
base bobj; base *bp1 = &bobj; base &br1 = bobj;
derived dobj; base *bp2 = &dobj; base &br2 = dobj;
// (a) base::print();
bobj.print(std::cout);
// (b) derived::print();
dobj.print(std::cout);
// (c) base::name();
bp1->name();
// (d) base::name();
bp2->name();
// (e) base::print();
br1.print(std::cout);
// (f) derived::print();
br2.print(std::cout);
return 0;
}