Constructors and Destructors in Inheritance


Every time an object is defined, a constructor must be called to create the memory for the object. An object of a derived class contains not only memory for the derived class attributes but also for the base class attributes. The question arises then: What happens when an object of a derived class is defined? How are the base class' attributes constructed?

Each derived class constructor is dependent on one of the base class constructors. That is every time a derived class constructor is called, a base class constructor must be called as well. Sometimes the base class constructor is the default constructor and sometimes it is a non default constructor.

A special problem may occur when the base class has only an explicit constructor. In this case, it is necessary for the derived class to have an explicit constructor as well. This is especially true when the only constructor the base class has is a non default constructor.

The definition of a serial derived class constructor whose base class constructors are non default constructors is:

image from book

 derivedClassName(signature) : baseClass(args) {   ... } 

image from book

Note that in the definition of the derived constructor above, there appears to be a call to the base class' constructor, and after the base class constructor's name there appears an argument list. The argument list of the base class' constructor is the signature of an execution/call of that constructor and not the signature of a definition or a declaration. The definition of the base class' constructor only appears in the base class.

In addition, each argument that appears in the base constructor's signature above must also appear in the signature of the derived constructor' definition. Further, any arguments that are listed in the argument list of the base class' constructor may also be used within the body of the derived class' constructor.

While the example above shows arguments, it should be noted that the signature of the derived class' constructor and the listed base class' constructor may be void. If the signature of the base class is void (i.e. it is the default constructor), then the base class' constructor is usually not listed in the definition of the derived class' constructor. Although it is not listed, the default constructor would be called.

The relationship between the constructors of the base and the constructors of the derived class is more complex than the general discussion above presents. To clarify this relationship, you should check out each of the following examples:

  • constructor1.cpp (both the base class and the derived class use their default constructors to define derived class objects)

  • constructor2.cpp (the derived class constructor has a non default constructor which uses the default constructor of the base class to define derived class objects)

  • constructor3.cpp (the derived class constructor has a non default constructor which uses the default constructor of the base class to define derived class objects)

  • constructor4.cpp (the derived class constructor has a non default constructor which uses the default constructor of the base class to define derived class objects)

  • constructor5.cpp (the derived class has two constructors one which is a default and another which is a non default constructor both of which use the default constructor of the base class to define derived class objects)

  • constructor6.cpp (the derived class constructor has a default and a non default constructor the first of which uses a default constructor and the second user a non default constructor of the base class to define derived class objects)

  • constructor7.cpp

  • constructor8.cpp

  • constructor9.cpp

After viewing these examples, what general conclusions about the relationships of constructors of the classes involved in a serial inheritance chain?

image from book

When considering an object of a derived class, it should be kept in mind that the derived class' object has memory associated with it that is required to store the base class part of the object as can be seen in the graphic above. As a result, the constructor of the derived class or some other member function must be able to initialize and to access that part of the derived object's memory which is associated with the base class as well as the memory associated with the derived class.

When an object of a derived class is defined and then when the derived object goes out of scope, the question arises as to which constructor and in the second case which destructor is called first.

When an object of the derived class is defined, the base class constructor is called first (and in case of multiple extensions they are called from left to right when viewing the class's definition).

When an object of the derived class goes out of scope, then the destructor of the derived class is called first followed by a call to the destructor of the base class (and in the case of multiple extensions the calls are in reverse order to the order that the constructors were called).

For an example to observe the order constructors and destructors are called for derived classes see consdest.cpp




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net