-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample.cpp
More file actions
187 lines (146 loc) · 4.89 KB
/
ConceptualExample.cpp
File metadata and controls
187 lines (146 loc) · 4.89 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// ===========================================================================
// ConceptualExample.cpp // Abstract Classes vs Interfaces in C++
// ===========================================================================
#include <iostream>
#include <string>
namespace AbstractClassVsInterface {
// interface declaration
class Interface
{
public:
virtual ~Interface() {};
virtual void method_first() = 0; // only 'abstract' methods
virtual void method_second() = 0; // only 'abstract' methods
};
// abstract class declaration
class AbstractClass
{
public:
virtual ~AbstractClass() {}
AbstractClass(const std::string& message)
: m_message{ message }
{}
virtual void method_third() = 0; // 'abstract' method
virtual void method_fourth() // (virtual) method with implementation
{
std::cout << m_message << std::endl;
}
void method_fifth() const // (final) method
{
}
protected:
std::string m_message; // some data
};
// abstract class inheriting from an interface
class AnotherAbstractClass : public Interface
{
public:
AnotherAbstractClass(double value) : m_value { value } {}
void method_first() override
{
std::cout << m_value << std::endl;
}
protected:
double m_value;
};
// concrete class inheriting from an abstract class
class ConcreteClass : public AnotherAbstractClass
{
public:
ConcreteClass() : ConcreteClass{ 1.0 } {}
ConcreteClass(double value)
: AnotherAbstractClass{ value }, m_anotherValue{} {}
ConcreteClass(double value1, double value2)
: AnotherAbstractClass{ value1 }, m_anotherValue{ value2 } {}
void method_second() override
{
std::cout << m_value << ", " << m_anotherValue << std::endl;
}
private:
double m_anotherValue;
};
// concrete class inheriting directly from an interface
class AnotherConcreteClass : public Interface
{
public:
AnotherConcreteClass() : m_oneMoreValue{ 1.0 } {}
AnotherConcreteClass(double value) : m_oneMoreValue{ value } {}
void method_first() override
{
std::cout << m_oneMoreValue << std::endl;
}
void method_second() override
{
method_first();
}
void my_method()
{
}
private:
double m_oneMoreValue;
};
// =======================================================================
static Interface* getInterface()
{
AnotherConcreteClass* impl = new AnotherConcreteClass();
// or
// Interface* impl = new AnotherConcreteClass();
return impl;
}
// =======================================================================
static void test_client_of_interface()
{
Interface* ip = getInterface();
ip->method_first();
delete ip;
}
}
// ===========================================================================
void test_conceptual_example()
{
using namespace AbstractClassVsInterface;
// Interface obj1; // Error
// AbstractClass obj2; // Error
// AnotherAbstractClass obj3; // Error
ConcreteClass obj4{ 1.5, 2.5 };
obj4.method_first();
obj4.method_second();
AnotherConcreteClass obj5{ 3.5 };
obj5.method_first();
obj5.method_second();
// ---------------------------------------------
// using references
Interface& obj01{ obj4 };
obj01.method_first();
obj01.method_second();
// AbstractClass& obj02{ obj4 }; // Error - no relationship with interface 'Interface'
AnotherAbstractClass& obj03{ obj4 };
obj03.method_first();
obj03.method_second();
ConcreteClass obj04{ obj4 };
obj04.method_first();
obj04.method_second();
AnotherConcreteClass obj05{ obj5 };
obj05.method_first();
obj05.method_second();
// ---------------------------------------------
// using pointers
Interface* obj001{ &obj4 };
obj001->method_first();
obj001->method_second();
// AbstractClass* obj002{ &obj4 }; // Error
AnotherAbstractClass* obj003{ &obj4 };
obj003->method_first();
obj003->method_second();
ConcreteClass* obj004{ &obj4 };
obj004->method_first();
obj004->method_second();
AnotherConcreteClass* obj005{ &obj5 };
obj005->method_first();
obj005->method_second();
// ---------------------------------------------
test_client_of_interface();
}
// ===========================================================================
// End-of-File
// ===========================================================================