Skip to content

Commit 223ecef

Browse files
authored
Merge pull request #5 from urboob21/dev_branch
2025_11_10
2 parents 6360dc1 + bcdae3b commit 223ecef

6 files changed

Lines changed: 522 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ set(APP_SOURCES
8787
"src/core/datatypes/CStruct.cpp"
8888
"src/core/datatypes/CUnion.cpp"
8989
"src/core/datatypes/TypeConVersions.cpp"
90+
"src/core/datatypes/class/CConstructors.cpp"
91+
"src/core/datatypes/class/CDestructors.cpp"
92+
"src/patterns/structural/Adapter.cpp"
9093
)
9194

9295
# Test files

docs/uml/patterns_structural_adapter.drawio.svg

Lines changed: 4 additions & 0 deletions
Loading

src/core/basics/InitializeVariable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct Foo
2525
cout << "Default constructor/ default init\n";
2626
}
2727

28-
// explicit Foo(int)
28+
// Foo(int)
2929
explicit Foo(int)
3030
{
3131
cout << "Constructor called with int / copy init\n";
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
// *1. Members initializer list constructor
5+
namespace InitializerList
6+
{
7+
class CConstructors
8+
{
9+
private:
10+
int m_x, m_y, m_z;
11+
12+
public:
13+
CConstructors(int x, int y) : m_x(x), m_y(y)
14+
{
15+
cout << "Called CConstructors(int x, int y) : m_x(x), m_y(y) \n";
16+
// m_z{0}; // error, it already initialized, so only can do assigment
17+
// m_z(0); // error, it already initialized, so only can do assigment
18+
m_z = 0;
19+
}
20+
21+
// using brace init
22+
CConstructors(int x, int y, int z) : m_x{x}, m_y{y}, m_z{z}
23+
{
24+
cout << "Called CConstructors(int x, int y, int z) : m_x{x}, m_y{y}, m_z{z} \n";
25+
}
26+
27+
// default arguments : must always be the RIGHTMOST parameters
28+
explicit CConstructors(int x = 1)
29+
{
30+
cout << "Called CConstructors(int x = 1) \n";
31+
m_x = x;
32+
m_y = 0;
33+
m_z = 0;
34+
}
35+
36+
// CConstructors() = default; error:
37+
// ‘InitializerList::CConstructors::CConstructors(int)’ cannot be overloaded with
38+
// ‘InitializerList::CConstructors::CConstructors(int x = 1)’
39+
40+
void print() const
41+
{
42+
cout << "m_x = " << m_x << ", m_y = " << m_y << ", m_z = " << m_z << "\n";
43+
}
44+
};
45+
46+
void constructers()
47+
{
48+
cout << "\n--- InitializerList Constructer Examples ---\n";
49+
CConstructors obj;
50+
CConstructors obj1 = CConstructors(1, 2);
51+
obj1.print();
52+
53+
CConstructors obj2 = CConstructors(3, 4, 5);
54+
obj2.print();
55+
}
56+
}
57+
58+
// *2. Default constructor: is a constructor that accepts no arguments.
59+
namespace Default
60+
{
61+
class UConstructors
62+
{
63+
public:
64+
// User-defined default constructor without argument
65+
UConstructors()
66+
{
67+
cout << "Called UConstructors() \n";
68+
}
69+
};
70+
71+
class IConstructors
72+
{
73+
public:
74+
// Implicit default constructor is generated by the compiler when the class has no user-declared constructors
75+
};
76+
77+
class EConstructors
78+
{
79+
public:
80+
// we already create the constructor ourselves
81+
// EConstructors(int a)
82+
explicit EConstructors(float a) // explicit -> [[maybe_unused]] EConstructors obj2 = 1; [ERROR]
83+
84+
{
85+
cout << "Called explicit EConstructors(int a) \n";
86+
}
87+
88+
// Explicit default constructor : also want the compiler to generate the default constructor.
89+
EConstructors() = default;
90+
};
91+
92+
void constructers()
93+
{
94+
cout << "\n--- Default Constructer Examples ---\n";
95+
[[maybe_unused]] UConstructors obj1;
96+
// [[maybe_unused]] UConstructors obj2(); // wrong, this is function declare
97+
// FYI:
98+
// void outer()
99+
// {
100+
// void helper();
101+
// helper(); // defined later in the same file
102+
// }
103+
104+
[[maybe_unused]] UConstructors obj3{};
105+
106+
[[maybe_unused]] IConstructors obj4;
107+
[[maybe_unused]] IConstructors obj6{};
108+
109+
[[maybe_unused]] EConstructors obj7;
110+
[[maybe_unused]] EConstructors obj9{};
111+
112+
[[maybe_unused]] EConstructors obj10(1.2);
113+
[[maybe_unused]] EConstructors obj11{2};
114+
}
115+
}
116+
117+
// *3. Delegate constructor: allow to delegate initialization to another constructor
118+
namespace Delegate
119+
{
120+
class CConstructor
121+
{
122+
private:
123+
int m_x;
124+
int m_y;
125+
126+
public:
127+
CConstructor(int x, int y) : m_x{x}, m_y{y}
128+
{
129+
cout << "Called CConstructor(int x, int y) : m_x{x}, m_y{y} \n";
130+
}
131+
132+
explicit CConstructor(int x) : CConstructor{x, 1}
133+
{
134+
cout << "Called CConstructor(int x):CConstructor(x,0) \n";
135+
}
136+
137+
CConstructor() : CConstructor(0)
138+
{
139+
cout << "Called CConstructor() : CConstructor(0) \n";
140+
}
141+
142+
void print() const
143+
{
144+
cout << "m_x = " << m_x << ", m_y = " << m_y << "\n";
145+
}
146+
};
147+
148+
void constructors()
149+
{
150+
cout << "\n--- Delegate Constructer Examples ---\n";
151+
CConstructor obj1 = CConstructor();
152+
obj1.print();
153+
}
154+
}
155+
156+
// *4. Copy constructor: initialize an copy object with an existing object
157+
namespace Copy
158+
{
159+
class ICConstructor // C++ will create a public implicit copy constructor for us if we do not provide a one.
160+
{
161+
private:
162+
int m_x;
163+
int m_y;
164+
165+
public:
166+
ICConstructor(int x, int y) : m_x{x}, m_y{y}
167+
{
168+
cout << "Called ICConstructor(int x, int y) : m_x{x}, m_y{y} \n";
169+
}
170+
171+
void print() const
172+
{
173+
cout << "m_x = " << m_x << ", m_y = " << m_y << "\n";
174+
}
175+
};
176+
177+
class ECConstructor // explicitly define our own copy constructor
178+
{
179+
private:
180+
int m_x;
181+
int m_y;
182+
183+
public:
184+
ECConstructor(int x, int y) : m_x{x}, m_y{y}
185+
{
186+
cout << "Called ICConstructor(int x, int y) : m_x{x}, m_y{y} \n";
187+
}
188+
189+
// using `default` keyword
190+
// ECConstructor(const ECConstructor &ref) = default;
191+
192+
ECConstructor(const ECConstructor &ref) : m_x{ref.m_x}, m_y{ref.m_x}
193+
{
194+
cout << "Called ECConstructor(const ECConstructor& ref) : m_x{ref.m_x}, m_y{ref.m_x} \n";
195+
}
196+
197+
void print() const
198+
{
199+
cout << "m_x = " << m_x << ", m_y = " << m_y << "\n";
200+
}
201+
};
202+
203+
class DECConstructor // Delete the copy constructor so no copies can be made
204+
{
205+
private:
206+
int m_x;
207+
int m_y;
208+
209+
public:
210+
DECConstructor(int x, int y) : m_x{x}, m_y{y}
211+
{
212+
cout << "Called DECConstructor(int x, int y) : m_x{x}, m_y{y} \n";
213+
}
214+
215+
// using `delete` keyword
216+
DECConstructor(const DECConstructor &fraction) = delete;
217+
218+
void print() const
219+
{
220+
cout << "m_x = " << m_x << ", m_y = " << m_y << "\n";
221+
}
222+
};
223+
224+
void constructors()
225+
{
226+
cout << "\n--- Copy Constructer Examples ---\n";
227+
ICConstructor obj1 = ICConstructor(1, 2);
228+
ICConstructor obj2{obj1};
229+
obj1.print();
230+
obj2.print();
231+
232+
ECConstructor obj3 = ECConstructor(3, 4);
233+
ECConstructor obj4{obj3};
234+
obj3.print();
235+
obj4.print();
236+
237+
DECConstructor obj5 = DECConstructor(5, 6);
238+
// DECConstructor obj6{obj5}; error
239+
obj5.print();
240+
}
241+
}
242+
243+
struct CConstructorsAutoRuner
244+
{
245+
CConstructorsAutoRuner()
246+
{
247+
InitializerList::constructers();
248+
Default::constructers();
249+
Delegate::constructors();
250+
Copy::constructors();
251+
}
252+
};
253+
254+
static CConstructorsAutoRuner instance;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
// *1. Basic Destructor
5+
namespace Basic
6+
{
7+
class CDestructors
8+
{
9+
public:
10+
CDestructors()
11+
{
12+
cout << "Called CDestructors() \n";
13+
}
14+
15+
~CDestructors()
16+
{
17+
cout << "Called ~CDestructors() \n";
18+
}
19+
20+
// Using `default` keyword
21+
// ~CDestructors() = default;
22+
23+
// Using `delete` // I forbid this destructor
24+
// ~CDestructors() = delete;
25+
};
26+
27+
void destructers()
28+
{
29+
cout << "\n--- Basic Destructer Examples ---\n";
30+
{
31+
CDestructors obj;
32+
}
33+
}
34+
}
35+
36+
// *2. Virtual Destructor
37+
namespace Virtual
38+
{
39+
class CDestructorsBase // final => cannot inherit
40+
{
41+
public:
42+
CDestructorsBase()
43+
{
44+
cout << "Called CDestructorsBase() \n";
45+
}
46+
47+
virtual ~CDestructorsBase()
48+
{
49+
cout << "Called ~CDestructorsBase() \n";
50+
}
51+
52+
// Using `default` keyword
53+
// ~CDestructorsBase() = default;
54+
};
55+
56+
class CDestructorsDerived : public CDestructorsBase
57+
{
58+
public:
59+
CDestructorsDerived()
60+
{
61+
cout << "Called CDestructorsDerived() \n";
62+
}
63+
64+
~CDestructorsDerived() override
65+
{
66+
cout << "Called ~CDestructorsDerived() \n";
67+
}
68+
};
69+
70+
void destructers()
71+
{
72+
cout << "\n--- Virtual Destructer Examples ---\n";
73+
CDestructorsDerived *derived = {new CDestructorsDerived()};
74+
CDestructorsBase *base{derived};
75+
delete base;
76+
// without virtual -> only call ~CDestructorsBase()
77+
// with virtual -> call ~CDestructorsBase() && ~CDestructorsDerived()
78+
}
79+
}
80+
81+
struct CDestructorsAutoRuner
82+
{
83+
CDestructorsAutoRuner()
84+
{
85+
Basic::destructers();
86+
Virtual::destructers();
87+
}
88+
};
89+
90+
static CDestructorsAutoRuner instance;

0 commit comments

Comments
 (0)