C++ Tutorial

Class and Object

Reference

Inheritance and Derivation

Polymorphism and Virtual Functions

Operator Overloading

Template

Exception

Object Oriented Advanced

Input/Output Stream

File Operations

Constructors for Base Class And Derived Class in C++

In this tutorial, we will discuss constructors in the context of inheritance in C++, particularly how base class and derived class constructors work.

  • Introduction to Constructors and Inheritance: In C++, constructors are special member functions that are called automatically when an object is created. They initialize the object and set its data members to initial values. Inheritance is the process by which one class inherits properties and methods from another class, creating a hierarchy of classes.

  • Constructors in Base Class: When a class is inherited, the base class constructor is called before the derived class constructor. If no constructor is explicitly provided in the base class, the compiler automatically generates a default constructor for you.

Base Class Example:

class Base {
public:
    Base() {
        std::cout << "Base class constructor called" << std::endl;
    }
};
  • Constructors in Derived Class: The derived class constructor is responsible for calling the appropriate base class constructor. If no constructor is specified for the derived class, the compiler will automatically generate a default constructor that calls the base class constructor.

Derived Class Example:

class Derived : public Base {
public:
    Derived() {
        std::cout << "Derived class constructor called" << std::endl;
    }
};
  • Constructor Initialization Lists: To explicitly specify which base class constructor should be called by the derived class constructor, you can use a constructor initialization list. An initialization list comes after the derived class constructor's signature and is introduced by a colon followed by the base class constructor and its arguments.

Example:

class Derived : public Base {
public:
    Derived(int x) : Base(x) {
        std::cout << "Derived class constructor called with value: " << x << std::endl;
    }
};
  • Example: Let's create a simple example to demonstrate the interaction between base and derived class constructors.
#include <iostream>

class Base {
public:
    Base() {
        std::cout << "Base class constructor called" << std::endl;
    }

    Base(int x) {
        std::cout << "Base class constructor called with value: " << x << std::endl;
    }
};

class Derived : public Base {
public:
    Derived() {
        std::cout << "Derived class constructor called" << std::endl;
    }

    Derived(int x) : Base(x) {
        std::cout << "Derived class constructor called with value: " << x << std::endl;
    }
};

int main() {
    Derived d1; // Output: Base class constructor called
                //         Derived class constructor called

    Derived d2(5); // Output: Base class constructor called with value: 5
                   //         Derived class constructor called with value: 5

    return 0;
}
  • Summary: In this tutorial, we discussed the constructors in base and derived classes and their interaction in C++. We explored how the base class constructor is called before the derived class constructor and the usage of constructor initialization lists to specify which base class constructor to call.
  1. Base class constructor calling in C++:

    • Description: Demonstrates how the constructor of a base class is called when an object of the derived class is created.
    • Example Code:
      class Base {
      public:
          Base() {
              // Base class constructor
          }
      };
      
      class Derived : public Base {
      public:
          // Derived class constructor
          Derived() {
              // Derived class constructor body
          }
      };
      
  2. Derived class constructor in C++ with base class initialization:

    • Description: Shows how to initialize the base class part of a derived class constructor explicitly.
    • Example Code:
      class Base {
      public:
          int baseData;
      
          Base(int value) : baseData(value) {
              // Base class constructor
          }
      };
      
      class Derived : public Base {
      public:
          int derivedData;
      
          // Derived class constructor with base class initialization
          Derived(int baseValue, int derivedValue) : Base(baseValue), derivedData(derivedValue) {
              // Derived class constructor body
          }
      };
      
  3. Base and derived class constructor overloading in C++:

    • Description: Demonstrates constructor overloading in both base and derived classes.
    • Example Code:
      class Base {
      public:
          int baseData;
      
          // Constructor overloading in the base class
          Base() : baseData(0) {}
          Base(int value) : baseData(value) {}
      };
      
      class Derived : public Base {
      public:
          int derivedData;
      
          // Constructor overloading in the derived class
          Derived() : Base(), derivedData(0) {}
          Derived(int baseValue, int derivedValue) : Base(baseValue), derivedData(derivedValue) {}
      };
      
  4. Using constructors with polymorphism in C++:

    • Description: Demonstrates how constructors can be used in conjunction with polymorphism, where base class pointers can point to derived class objects.
    • Example Code:
      class Base {
      public:
          virtual void display() {
              // Base class display function
          }
      };
      
      class Derived : public Base {
      public:
          void display() override {
              // Derived class display function
          }
      };
      
      int main() {
          Base* ptr = new Derived(); // Using base class pointer to a derived class object
          ptr->display(); // Calls the overridden display function in the derived class
      
          delete ptr;
          return 0;
      }