Explanation of Inheritance in C++

Explanation of Inheritance in C++

Hello everyone, in this article we are going to talk about Inheritance concept of Object Oriented Programming Languages within C++ with examples.

Let's get started.

Inheritance is one of most important concept in object-oriented programming (OOP) that allows us to create new classes based on existing ones. Within C++, Inheritance is a powerful feature that allows us to reuse the code and create a class hierarchy. Also we are able to improve the code quality. With this article you will see the basics of inheritance in C++ and code examples for beginners.

What is Inheritance?

Inheritance is a relationship between two classes where one class, known as the base class or parent class, provides some of its attributes and methods to another class, known as the derived class or child class. The derived class can then use these attributes and methods, as well as add its own.

Below you can see how to inherit a class in C++

class ParentClass {
  // ... 
  // Definitions
  // ... 
};

class DerivedClass : public ParentClass {
  // ... 
  // Definitions and usages of parent class
  // ... 
};

In C++, there are 3 access types of inheritance:

  • Public Inheritance: In public inheritance, the public members of the base class become public members of the derived class, and the protected members of the base class become protected members of the derived class. Private members of the base class are not accessible from the derived class.
  • Private Inheritance: In private inheritance, the public and protected members of the base class become private members of the derived class. Private members of the base class are not accessible from the derived class.
  • Protected Inheritance: In protected inheritance, the public and protected members of the base class become protected members of the derived class. Private members of the base class are not accessible from the derived class.
Below you can see access modes of inheritance in C++

class ParentClass {
  // ... 
  // Definitions
  // ... 
};

class DerivedClass : private ParentClass {
  // Can Access private members of parent class
};

class DerivedClass1 : public ParentClass {
  // Can Access public members of parent class
};

class DerivedClass2 : protected ParentClass {
  // Can Access protected members of parent class
};

Important Note: Please do not confuse Access Modes and Inheritance Types in C++.

Now lets talk about inheritance types of C++. There are five types of inheritance:

  • Single Inheritance: Single inheritance is the most commonly used type of inheritance in which a derived class is derived from a single base class. It means that a derived class can inherit only one base class.
  • Multiple Inheritance: Multiple inheritance is a type of inheritance in which a derived class is derived from multiple base classes.
  • Hierarchical Inheritance: Hierarchical inheritance is a type of inheritance in which a derived class is derived from a single base class, but multiple derived classes can be derived from the same base class.
  • Multi-level Inheritance: A child class will be derived from a parent class and this parent class will be derived from another parent class. So with this inheritance there will be a hierarchi between the classes and they will use same base.
  • Hybrid Inheritance: Hybrid Inheritance is a type of inheritance in C++ that combines multiple types of inheritance, such as Hierarchical Inheritance and Multiple Inheritance, to create a complex inheritance hierarchy.

Now, Let's make more examples for better understanding of Inheritance in C++


class BaseClass {
public:
    void printBase() {
        cout << "This class is base." << endl;
    }
};

class DerivedClass1 {
public:
    void printDerived1() {
        cout << "This class is first derived class" << endl;
    }
};

class DerivedClass2 : public DerivedClass1 {
public:
    void printDerived2() {
        cout << "This class is second derived class" << endl;
    }
};

int main() {
    //Class initializations
    DerivedClass1 obj1;
    DerivedClass2 obj2;

    // Single Inheritance printing
    obj1.printBase();
    obj1.printDerived1();

    //Multi level Inheritance printing
    obj2.printBase();
    obj2.printDerived1();
    obj2.printDerived2();
    return 0;
}
In above example we can see two types of inheritance types:
  • Single Inheritance In here DerivedClass1 inherits only Base class and is able to use only public definitions of Base class. Because public access type is used for inheriting.
  • Multi-level Inheritance As you can see DerivedClass2 inherits DerivedClass1 which inherits Base Class. In this way DerivedClass2 is able to use all public definitions of above level inherited classes.

Now, Let's make another example


#include <iostream>
using namespace std;

// Base Classes
class Animal {
public:
    void speak() {
        cout << "This is base Animal class" << endl;
    }
};

class Plant {
public:
    void speak() {
        cout << "This is base Plant class" << endl;
    }
};

//Derived Classes
class Cat : public Animal {
public:
    void speak() {
        cout << "Meow" << endl;
    }
};

class Dog : public Animal {
public:
    void speak() {
        cout << "Woof" << endl;
    }
};

class Fungi : public Animal, public Plant {
public:
    void speak() {
        cout << "Whuuu" << endl;
    }
};

int main() {
    // Definitions and usages
    return 0;
}
In above example we can see two types of inheritance types:
  • Hierarchical Inheritance As you can see In here we have Animal Class is base and both Cat and Dog classes inherit the base Animal class. This type of inheritance is hierarchical inheritance. Multiple child classes inherit same base class.
  • Hybrid Inheritance and Multiple Inheritance We also have Fungi class which inherits both Animal and Plant classes. So this class is a hybrid class of both inherited classes. So in this type we will see multiple and hybrid inheritance together. Because It is demonstrating the hierarchical inheritance also

As a result we can use inheritance concept to increase the reusability and we can improve the grouping of the code.

That is basically all.

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...