In which of the following is the constructor of myClass called?
1. myClass class1;
2. myClass* class1;
3. myClass* class1 = new myClass;
Thanks a lot
Constructor is a special member function of a class which is called/invoked when the object is created. It has the same name as that of a class. Ashish Bardiya said: (Jul 3, 2013) Constructor is call when an object is created or initialize.
The constructors can be called explicitly or implicitly. The method of calling the constructor implicitly is also called the shorthand method. Example e = Example(0, 50); // Explicit call. Example e2(0, 50); // Implicit call.
If members have default constructors or constructor without parameter then these constructors are called automatically, otherwise parameterized constructors can be called using Initializer List. For example, see PROGRAM 1 and PROGRAM 2 and their output.
For multiple inheritance order of constructor call is, the base class's constructors are called in the order of inheritance and then the derived class's constructor.
But this code will not instantiate any object, as it does not compile. ;-) Try this one:
myClass class1;
myClass* class2;
myClass* class3 = new myClass;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With