31-01-2013, 11:44 AM
CONSTRUCTORS IN DERIVED CLASSES
CONSTRUCTORS IN DERIVED.pptx (Size: 103.52 KB / Downloads: 22)
The Constructor of a Derived Class
Derived classes don’t inherit constructors and destructors but still they can call constructors and destructors of base classes.
If we do not provide a constructor then the compiler creates a default constructor that does not include any parameter.
Derived-class constructor invokes base class constructor either
implicitly (via a base-class member initializer) or
explicitly (by calling the base classes default constructor).
If any base class contains a constructor with one or more arguments, then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructors.
Advantages
One of the primary advantages of using a base class constructor to initialize the base class members is that if the base class constructor is ever changed, both the base class and all inherited classes will automatically use the changes! This helps keep maintenance and duplicate code down.