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
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; } };
Derived Class Example:
class Derived : public Base { public: Derived() { std::cout << "Derived class constructor called" << std::endl; } };
Example:
class Derived : public Base { public: Derived(int x) : Base(x) { std::cout << "Derived class constructor called with value: " << x << std::endl; } };
#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; }
Base class constructor calling in C++:
class Base { public: Base() { // Base class constructor } }; class Derived : public Base { public: // Derived class constructor Derived() { // Derived class constructor body } };
Derived class constructor in C++ with base class initialization:
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 } };
Base and derived class constructor overloading in C++:
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) {} };
Using constructors with polymorphism in C++:
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; }