Home | | Object Oriented Programming and Data Structures | Using Constructors and Destructors in Derived Classes

Chapter: Object Oriented Programming and Data Structure : Inheritance and Polymorphism

Using Constructors and Destructors in Derived Classes

Object Oriented Programming and Data Structure - Inheritance and Polymorphism

Using Constructors and Destructors in Derived Classes

 

Base class initializer

 

Uses member-initializer syntax

Can be provided in the derived class constructor to call the base-class constructor explicitly

 

– Otherwise base class’ default constructor called implicitly

Base-class constructors and base-class assignment operators are not inherited by derived classes

– However, derived-class constructors and assignment operators can call still them Derived-class

constructor Calls the constructor for its base class first to initialize its base-class members

If the derived-class constructor is omitted, its default constructor calls the base-class’ default constructor

 

Destructors are called in the reverse order of constructor calls. Derived-class destructor is called before its base-class Destructor

 

Implicit Derived-Class Object to Base-Class Object Conversion

baseClassObject = derivedClassObject;

This will work

 

– Remember, the derived class object has more membersthan the base class object Extra data is not given to the base class

 

derivedClassObject = baseClassObject;

May not work properly

– Unless an assignment operator is overloaded in the derived class, data members exclusive to the derived class will be unassigned Base class has less data members than the derived class

– Some data members missing in the derived class object

 

Four ways to mix base and derived class pointers andobjects Referring to a base-class object with a base-class pointer

– Allowed

Referring to a derived-class object with a derived-class pointer

– Allowed

Referring to a derived-class object with a base-class pointer.

– Possible syntax error

 

– Code can only refer to base-class members, or syntax error Referring to a base-class object with a derived-class pointer

– Syntax error

 

– The derived-class pointer must first be cast to a base-class Pointer

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming and Data Structure : Inheritance and Polymorphism : Using Constructors and Destructors in Derived Classes |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.