Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
64 lines (51 loc) · 1.37 KB
/
Copy pathmain.cpp
File metadata and controls
64 lines (51 loc) · 1.37 KB
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
#include <stdio.h>
#include <string>
using namespace std;
class ChildA;
class ChildB;
class Handler {
public:
static void handle(ChildA& left, ChildA& right);
static void handle(ChildA& left, ChildB& right);
static void handle(ChildB& left, ChildA& right);
static void handle(ChildB& left, ChildB& right);
};
class Interface {
public:
Interface(const std::string& name_) : name(name_) {}
virtual ~Interface () {}
virtual void handle(Interface& i) = 0;
const std::string& getName() const { return name; }
private:
std::string name;
};
class ChildA : public Interface {
public:
ChildA(const std::string& name_) : Interface(name_) {}
virtual void handle(Interface& i) {
printf("%s: %s(%s)\n", getName().c_str(), __func__, i.getName().c_str());
}
};
class ChildB : public Interface {
public:
ChildB(const std::string& name_) : Interface(name_) {}
virtual void handle(Interface& i) {
printf("%s: %s(%s)\n", getName().c_str(), __func__, i.getName().c_str());
}
};
int main(int argc, const char *argv[])
{
Interface* a1 = new ChildA("A1");
Interface* a2 = new ChildA("A2");
Interface* b1 = new ChildB("B1");
Interface* b2 = new ChildB("B2");
a1->handle(*a2);
a2->handle(*b1);
b1->handle(*a1);
b2->handle(*b1);
delete a1;
delete a2;
delete b1;
delete b2;
return 0;
}