Constructor in derived class in C++

Constructor and Destructor in Derived class in C++

Constructor and Destructor in Derived class in C++

We know that constructor is invoked implicitly when an object is created. But do you think what will happen when we create object of derived class? 

1. In inheritance, When an object of derived class is created then constructor of derived class get executed and then it calls the constructor of base class.

2. If there is no default constructor present in parent class then not only we have to create constructor in child class but also we will have to call the constructor of parent class.

3. In order to call parameterized constructor of parent class, we need to create a constructor in child class and also we will have to call parameterized constructor with the help of child class constructor.

Syntax of calling parent class constructor using child class constructor 

class A
{
 public:
  A() {
  
   //constructor body
   
  }
 };
 
 class B: public A {
  public:
   B(): A() {
   
   //constructor body
  
  }
 };

For Example:-

class A
{
  int a;

public:
  A (int k)           //parameterized constructor of parent class.
  {
    a = k;
  }
};

class B: public A
{
  int b;
  
public:
  B(int x, int y):A(x)     //constructor of child class calling constructor of base class.
  {
    b = y;
  }
};

int main()
{
  B obj(2,3);
}

Points to Remember:-

1. In inheritance, the order of constructors calling is: from child class to parent class (child -> parent).

2. In inheritance, the order of constructors execution is: from parent class to child class (parent -> class).

3. In inheritance, the order of destructors calling is: from child class to parent class (child -> parent).

4. In inheritance, the order of destructors execution is: from child class to parent class (child -> parent).

For Example:- Destructor Example

#include
using namespace std;

class baseClass
{
public:
  baseClass()
  {
    cout << "I am baseClass constructor" << endl;
  }
  
  ~baseClass()
  {
    cout << "I am baseClass destructor" << endl;
  }
};

class derivedClass: public baseClass
{
public:
  derivedClass()
  {
    cout << "I am derivedClass constructor" << endl;
  }
  
  ~derivedClass()
  {
    cout <<" I am derivedClass destructor" << endl;
  }
};

int main()
{
  derivedClass D;
  return 0;
}

Points to Remember:-

 
class C: public A, public B
{
//...
};

1. Here, A class in inherited first, so constructor of class A is called first then the constructor of class B will be called next.

2. The destructor of derived class will be called first then destructor of base class which is mentioned in the derived class declaration is called from last towards first sequence wise.

For Example:- Constructor & Destructor in Multiple inheritance

#include
using namespace std;

class baseClass {
 public:
  baseClass1() {
   cout<<"I am baseClass1 constructor"<

  • Tweet
  • Share
  • Share
  • Share
  • Share

Any class type (whether declared with class-key class or struct) may be declared as derived from one or more base classes which, in turn, may be derived from their own base classes, forming an inheritance hierarchy.

The list of base classes is provided in the base-clause of the class declaration syntax. The base-clause consists of the character : followed by a comma-separated list of one or more base-specifiers.

attr(optional) class-or-decltype (1)
attr(optional) virtual class-or-decltype (2)
attr(optional) access-specifier class-or-decltype (3)
attr(optional) virtual access-specifier class-or-decltype (4)
attr(optional) access-specifier virtual class-or-decltype (5)

1) Specifies a non-virtual inheritance with default member accessibility.

2) Specifies a virtual inheritance with default member accessibility.

3) Specifies a non-virtual inheritance with given member accessibility.

4) Specifies a virtual inheritance with given member accessibility.

5) Same as 4), virtual and access-specifier can appear in any order.

attr - (since C++11) sequence of any number of attributes
access-specifier - one of private, public, or protected
class-or-decltype - one of
  • nested-name-specifier(optional) type-name
  • nested-name-specifier template simple-template-id

An elaborated type specifier cannot directly appear as class-or-decltype due to syntax limitations.

base-specifiers in a base-clause may be pack expansions.

A class or struct declared final cannot be denoted by class-or-decltype.

(since C++11)

If access-specifier is omitted, it defaults to public for classes declared with class-key struct and to private for classes declared with class-key class.

struct Base
{
    int a, b, c;
};
 
// every object of type Derived includes Base as a subobject
struct Derived : Base
{
    int b;
};
 
// every object of type Derived2 includes Derived and Base as subobjects
struct Derived2 : Derived
{
    int c;
};

Classes denoted by class-or-decltype's listed in the base-clause are direct base classes. Their bases are indirect base classes. The same class cannot be specified as a direct base class more than once, but the same class can be both direct and indirect base class.

Each direct and indirect base class is present, as base class subobject, within the object representation of the derived class at ABI-dependent offset. Empty base classes usually do not increase the size of the derived object due to empty base optimization. The constructors of base class subobjects are called by the constructor of the derived class: arguments may be provided to those constructors in the member initializer list.

Contents

  • 1 Virtual base classes
  • 2 Public inheritance
  • 3 Protected inheritance
  • 4 Private inheritance
  • 5 Member name lookup
  • 6 Defect reports
  • 7 See also

[edit] Virtual base classes

For each distinct base class that is specified virtual, the most derived object contains only one base class subobject of that type, even if the class appears many times in the inheritance hierarchy (as long as it is inherited virtual every time).

struct B { int n; };
class X : public virtual B {};
class Y : virtual public B {};
class Z : public B {};
 
// every object of type AA has one X, one Y, one Z, and two B's:
// one that is the base of Z and one that is shared by X and Y
struct AA : X, Y, Z
{
    AA()
    {
        X::n = 1; // modifies the virtual B subobject's member
        Y::n = 2; // modifies the same virtual B subobject's member
        Z::n = 3; // modifies the non-virtual B subobject's member
 
        std::cout << X::n << Y::n << Z::n << '\n'; // prints 223
    }
};

An example of an inheritance hierarchy with virtual base classes is the iostreams hierarchy of the standard library: std::istream and std::ostream are derived from std::ios using virtual inheritance. std::iostream is derived from both std::istream and std::ostream, so every instance of std::iostream contains a std::ostream subobject, a std::istream subobject, and just one std::ios subobject (and, consequently, one std::ios_base).

All virtual base subobjects are initialized before any non-virtual base subobject, so only the most derived class calls the constructors of the virtual bases in its member initializer list:

struct B
{
    int n;
 
    B(int x) : n(x) {}
};
 
struct X : virtual B { X() : B(1) {} };
struct Y : virtual B { Y() : B(2) {} };
struct AA : X, Y     { AA() : B(3), X(), Y() {} };
 
// the default constructor of AA calls the default constructors of X and Y
// but those constructors do not call the constructor of B because B is a virtual base
AA a; // a.n == 3
 
// the default constructor of X calls the constructor of B
X x;  // x.n == 1

There are special rules for unqualified name lookup for class members when virtual inheritance is involved (sometimes referred to as the rules of dominance).

[edit] Public inheritance

When a class uses public member access specifier to derive from a base, all public members of the base class are accessible as public members of the derived class and all protected members of the base class are accessible as protected members of the derived class (private members of the base are never accessible unless friended)

Public inheritance models the subtyping relationship of object-oriented programming: the derived class object IS-A base class object. References and pointers to a derived object are expected to be usable by any code that expects references or pointers to any of its public bases (see LSP) or, in DbC terms, a derived class should maintain class invariants of its public bases, should not strengthen any precondition or weaken any postcondition of a member function it overrides.

#include 
#include 
#include 
 
struct MenuOption { std::string title; };
 
// Menu is a vector of MenuOption: options can be inserted, removed, reordered...
// and has a title.
class Menu : public std::vector<MenuOption>
{
public:
    std::string title;
 
    void print() const
    {
        std::cout << title << ":\n";
        for (std::size_t i = 0, s = size(); i < s; ++i)
            std::cout << "  " << (i+1) << ". " << at(i).title << '\n';
    }
};
// Note: Menu::title is not problematic because its role is independent of the base class.
 
enum class Color { WHITE, RED, BLUE, GREEN };
 
void apply_terminal_color(Color) { /* OS-specific */ }
 
// THIS IS BAD!
// ColorMenu is a Menu where every option has a custom color.
class ColorMenu : public Menu
{
public:
    std::vector<Color> colors;
 
    void print() const
    {
        std::cout << title << ":\n";
 
        for (std::size_t i = 0, s = size(); i < s; ++i)
        {
            std::cout << "  " << (i+1) << ". ";
            apply_terminal_color(colors[i]);
            std::cout << at(i).title << '\n';
            apply_terminal_color(Color::WHITE);
        }
    }
};
// ColorMenu needs the following invariants that cannot be satisfied
// by publicly inheriting from Menu, for example:
// - ColorMenu::colors and Menu must have the same number of elements
// - To make sense, calling erase() should remove also elements from colors,
//   in order to let options keep their colors
// Basically every non-const call to a std::vector method will break the invariant
// of the ColorMenu and will need fixing from the user by correctly managing colors.
 
int main()
{
    ColorMenu color_menu;
 
    // The big problem of this class is that we must keep ColorMenu::Color
    // in sync with Menu.
    color_menu.push_back(MenuOption{"Some choice"});
 
    // color_menu.print(); // ERROR! colors[i] in print() is out of range
 
    color_menu.colors.push_back(Color::RED);
 
    color_menu.print(); // OK: colors and Menu has the same number of elements
}

[edit] Protected inheritance

When a class uses protected member access specifier to derive from a base, all public and protected members of the base class are accessible as protected members of the derived class (private members of the base are never accessible unless friended)

Protected inheritance may be used for "controlled polymorphism": within the members of Derived, as well as within the members of all further-derived classes, the derived class IS-A base: references and pointers to Derived may be used where references and pointers to Base are expected.

[edit] Private inheritance

When a class uses private member access specifier to derive from a base, all public and protected members of the base class are accessible as private members of the derived class (private members of the base are never accessible unless friended).

Private inheritance is commonly used in policy-based design, since policies are usually empty classes, and using them as bases both enables static polymorphism and leverages empty-base optimization.

Private inheritance can also be used to implement the composition relationship (the base class subobject is an implementation detail of the derived class object). Using a member offers better encapsulation and is generally preferred unless the derived class requires access to protected members (including constructors) of the base, needs to override a virtual member of the base, needs the base to be constructed before and destructed after some other base subobject, needs to share a virtual base or needs to control the construction of a virtual base. Use of members to implement composition is also not applicable in the case of multiple inheritance from a parameter pack or when the identities of the base classes are determined at compile time through template metaprogramming.

Similar to protected inheritance, private inheritance may also be used for controlled polymorphism: within the members of the derived (but not within further-derived classes), derived IS-A base.

template<typename Transport>
class service : private Transport // private inheritance from the Transport policy
{
public:
    void transmit()
    {
        this->send(...); // send using whatever transport was supplied
    }
};
 
// TCP transport policy
class tcp
{
public:
    void send(...);
};
 
// UDP transport policy
class udp
{
public:
    void send(...);
};
 
service<tcp> service(host, port); 
service.transmit(...); // send over TCP

[edit] Member name lookup

Unqualified and qualified name lookup rules for class members are detailed in name lookup.

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 1710 C++98 the syntax of class-or-decltype made it impossible to derive from
a dependent class where the template disambiguator is required
allowed template

[edit] See also

  • virtual functions
  • abstract classes

What is constructor in derived class?

Constructors in Derived Class in C++ If the class “A” is written before class “B” then the constructor of class “A” will be executed first. But if the class “B” is written before class “A” then the constructor of class “B” will be executed first.

Can derived class have constructor?

In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class.

Can constructors be defined overridden in derived class?

It does not have a return type and its name is same as the class name. But, a constructor cannot be overridden. If you try to write a super class's constructor in the sub class compiler treats it as a method and expects a return type and generates a compile time error.