|
| 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; |
0 commit comments