WikiGalaxy

Personalize

C++ Inheritance: Introduction

Understanding Inheritance

Inheritance is a fundamental concept in C++ that allows one class to inherit properties and behaviors from another. This promotes code reusability and establishes a hierarchical relationship between classes.

Base and Derived Classes

The class that is inherited from is called the base class, while the class that inherits is the derived class. This relationship forms the basis of inheritance in C++.


class Base {
public:
    void display() {
        cout << "Base class display function" << endl;
    }
};

class Derived : public Base {
public:
    void show() {
        cout << "Derived class show function" << endl;
    }
};

int main() {
    Derived obj;
    obj.display();
    obj.show();
    return 0;
}
    

Console Output:

Base class display function Derived class show function

Types of Inheritance

Single Inheritance

Single inheritance allows a derived class to inherit from only one base class. This is the simplest form of inheritance.

Multiple Inheritance

In multiple inheritance, a derived class can inherit from more than one base class. This can lead to complex hierarchies and potential ambiguities.


class Base1 {
public:
    void greet() {
        cout << "Hello from Base1" << endl;
    }
};

class Base2 {
public:
    void greet() {
        cout << "Hello from Base2" << endl;
    }
};

class Derived : public Base1, public Base2 {
public:
    void greet() {
        Base1::greet();
        Base2::greet();
    }
};

int main() {
    Derived obj;
    obj.greet();
    return 0;
}
    

Console Output:

Hello from Base1 Hello from Base2

Hierarchical Inheritance

Concept of Hierarchical Inheritance

In hierarchical inheritance, multiple derived classes inherit from a single base class. This structure is useful when several classes share common functionalities.


class Base {
public:
    void display() {
        cout << "Base class display function" << endl;
    }
};

class Derived1 : public Base {
public:
    void show1() {
        cout << "Derived1 class show1 function" << endl;
    }
};

class Derived2 : public Base {
public:
    void show2() {
        cout << "Derived2 class show2 function" << endl;
    }
};

int main() {
    Derived1 obj1;
    Derived2 obj2;
    obj1.display();
    obj1.show1();
    obj2.display();
    obj2.show2();
    return 0;
}
    

Console Output:

Base class display function Derived1 class show1 function Base class display function Derived2 class show2 function

Multilevel Inheritance

Understanding Multilevel Inheritance

Multilevel inheritance involves a chain of inheritance where a derived class becomes the base class for another derived class. This forms a linear hierarchy.


class Base {
public:
    void display() {
        cout << "Base class display function" << endl;
    }
};

class Intermediate : public Base {
public:
    void intermediateFunction() {
        cout << "Intermediate class function" << endl;
    }
};

class Derived : public Intermediate {
public:
    void show() {
        cout << "Derived class show function" << endl;
    }
};

int main() {
    Derived obj;
    obj.display();
    obj.intermediateFunction();
    obj.show();
    return 0;
}
    

Console Output:

Base class display function Intermediate class function Derived class show function

Hybrid Inheritance

Exploring Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance. It is used to overcome the limitations of single and multiple inheritance.


class Base1 {
public:
    void show() {
        cout << "Base1 show function" << endl;
    }
};

class Base2 {
public:
    void display() {
        cout << "Base2 display function" << endl;
    }
};

class Intermediate : public Base1 {
};

class Derived : public Intermediate, public Base2 {
};

int main() {
    Derived obj;
    obj.show();
    obj.display();
    return 0;
}
    

Console Output:

Base1 show function Base2 display function

Access Specifiers in Inheritance

Role of Access Specifiers

Access specifiers (public, protected, private) determine the accessibility of base class members in derived classes. They play a crucial role in inheritance.


class Base {
protected:
    int protectedVar;
public:
    int publicVar;
};

class Derived : public Base {
public:
    void show() {
        cout << "Protected Variable: " << protectedVar << endl;
        cout << "Public Variable: " << publicVar << endl;
    }
};

int main() {
    Derived obj;
    obj.publicVar = 10;
    obj.show();
    return 0;
}
    

Console Output:

Protected Variable: 0 Public Variable: 10

Virtual Inheritance

Avoiding Diamond Problem

Virtual inheritance is used to solve the diamond problem in multiple inheritance by ensuring a single instance of the base class.


class Base {
public:
    void show() {
        cout << "Base class function" << endl;
    }
};

class Derived1 : virtual public Base {
};

class Derived2 : virtual public Base {
};

class Final : public Derived1, public Derived2 {
};

int main() {
    Final obj;
    obj.show();
    return 0;
}
    

Console Output:

Base class function

Function Overriding

Overriding Base Class Methods

Function overriding allows a derived class to provide a specific implementation of a function that is already defined in its base class.


class Base {
public:
    virtual void display() {
        cout << "Display from Base class" << endl;
    }
};

class Derived : public Base {
public:
    void display() override {
        cout << "Display from Derived class" << endl;
    }
};

int main() {
    Base* ptr;
    Derived obj;
    ptr = &obj;
    ptr->display();
    return 0;
}
    

Console Output:

Display from Derived class

Abstract Classes and Pure Virtual Functions

Defining Abstract Classes

An abstract class is a class that cannot be instantiated and is designed to be inherited from. It typically contains at least one pure virtual function.


class AbstractBase {
public:
    virtual void pureVirtualFunction() = 0;
};

class Derived : public AbstractBase {
public:
    void pureVirtualFunction() override {
        cout << "Implementation of pure virtual function" << endl;
    }
};

int main() {
    Derived obj;
    obj.pureVirtualFunction();
    return 0;
}
    

Console Output:

Implementation of pure virtual function

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025