Home     Blog

Constructors in Derived Class

A constructor plays a vital role in initializing an object. An important note, while using constructors during inheritance, is that, as long as a base class constructor does not take any arguments, the derived class need not have a constructor function. However, if a 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 constructor. Remember, while applying inheritance, we usually create objects using derived class. Thus, it makes sense for the derived class to pass arguments to the base class constructor. When both the derived and base class contains constructors, the base constructor is executed first and then the constructor in the derived class is executed.

In case of multiple inheritance, the base class is constructed in the same order in which they appear in the declaration of the derived class. Similarly, in a multilevel inheritance, the constructor will be executed in the order of inheritance.

The derived class takes the responsibility of supplying the initial values to its base class. The constructor of the derived class receives the entire list of required values as its argument and passes them on to the base constructor in the order in which they are declared in the derived class. A base class constructor is called and executed before executing the statements in the body of the derived class.

The header line of the derived-constructor function contains two parts separated by a colon (:). The first part provides the declaration of the arguments that are passed to the derived class constructor and the second part lists the function calls to the base class.

Example:

A(a1, a2) invokes the base constructor A() and B(b1, b2) invokes another base class constructor B(). The constructor D() supply the values i.e. a1, a2, b1, b2 (to A() and B()) and to one of its own variables d1.

Hence, the assignment of the values will be as follows:

When an object of D is created, D object-name(5, 12, 7, 8, 30);

a1 <- 5 a2 <- 12 b1 <- 7 b2 <- 8 d1 <- 30

The constructors for a virtual base class are invoked before any non-virtual base classes. If there are multiple virtual base classes, then they are invoked in the order in which they are declared.

Example:

constructor in derived class clip image002 Constructors in Derived Class

constructor in derived class clip image004 Constructors in Derived Class

VN:F [1.9.17_1161]
Rating: 8.5/10 (2 votes cast)
Constructors in Derived Class, 8.5 out of 10 based on 2 ratings
Follow Daniel Memetic on

Comments (1)

 

  1. dantreliya sanjay says:

    class: public A
    {                                                    A() base constuctor
                                                        B() Derived consutructor       }

    class A:

     

     

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)

Leave a Reply

Related Posts

  • Constructors and Destructors

    A member function with the same name as its class is called a constructor. It is used to initialize the objects of that class-type with a some initial value. If a class has a constructor, each of the objects of that class will be initialized before they are used within the program. A constructor for [...]...


  • Type Conversion – Class to Class

    Now that we have understood how to convert basic data types to class types and vice-versa, it is time to learn how to convert objects of one class type to another class type. The conversion between objects of different classes can be done using either a one-argument constructor or a conversion function. The choice depends [...]...


  • Inheritance

    Inheritance is what happens when a subclass receives variables or methods from a superclass. It is also when one class passes down its properties to another class. The main reason for inheritance is reusability. Distinguishing a new class from an existing class is called derivation. The old class is referred to as the base class [...]...


  • Constructor

    A constructor is a special method for initializing a new instance of a class. The constructor method for a class will have the same name as the class. Constructor Example This example shows a class and a constructor named Circle: public class Circle { public int x = 0; public int y = 0; public [...]...


  • Constructors that Allocate Memory Dynamically

    Constructors can be used to initialize member objects as well as allocate memory. This can allow an object to use only that amount of memory that is required immediately. This memory allocation at run-time is also known as dynamic memory allocation. The new operator is used for this purpose. Sample Program The program below shows [...]...