-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabc.cpp
68 lines (58 loc) · 984 Bytes
/
abc.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <fstream>
#include <iostream>
class ABC {
public:
ABC() {}
virtual ~ABC() {}
virtual void printOn(std::ostream &os) const=0;
};
class Imp1 : public ABC {
public:
void printOn(std::ostream &os) const
{
os << "This is an Imp1 instance";
}
};
class Imp2 : public ABC {
public:
void printOn(std::ostream &os) const
{
os << "This is an Imp2 instance";
}
};
class Master {
public:
explicit Master(ABC *_abc) : abc(_abc) {}
virtual ~Master() {}
ABC *abc;
void printOn(std::ostream &os) const
{
abc->printOn(os);
}
};
class Derived1 : public Master {
public:
Derived1() : Master(new Imp1) {}
~Derived1()
{
delete abc;
}
};
class Derived2 : public Master {
public:
Derived2() : Master(new Imp2) {}
~Derived2()
{
delete abc;
}
};
int main()
{
Derived1 master1;
Derived2 master2;
master1.printOn(std::cout);
std::cout << std::endl;
master2.printOn(std::cout);
std::cout << std::endl;
return 0;
}