Constructors are different from other class methods in that they create new objects, whereas other methods are invoked by existing objects. This is one reason constructors aren’t inherited. Inheritance means a derived object can use a base-class method, but, in the case of constructors, the object doesn’t exist until after the constructor has done its work.
Does a constructor create new object or when a object is called the constructor is called immediately?
It is said that a constructor and destructor is not inherited from the base class to the derived class but is the program below a contradiction, we are creating an object of the derived class but it outputs constructor and destructor of the base class also?
class A{ public: A(){ cout<< Const A called<<endl; } ~A(){ cout<< Dest A called <<endl; } }; Class B : public A{ public: B(){ cout<< Const B called <<endl; } ~B(){ cout<< Dest B called <<endl; } }; int main(){ B obj; return 0; } Output:
Const A called
Const B called
Dest B called
Dest A called
A, then see ifBhas inherited a constructor with a parameter.