Inheritance in C++

Share Now

Inheritance in C++ is an important concept of Object-Oriented Programming (OOP) that allows one class to acquire the properties and behaviors of another class. The class that inherits the features is called the derived class, while the class whose features are inherited is known as the base class.

Inheritance in C++

One of the most powerful features of the object-orientated program. It allows a new class (called the derived class) to reuse the properties and behaviours of an existing class (called the base class), meaning inheritance has a feature to inherit the properties of another class or classes. The main purpose of inheritance is code reusability.

Quick Table

SpecifierInside ClassDerived ClassOutside Object
publicYesYesYes
protectedYesYesNo
privateYesNoNo

Different Forms of Inheritance

C++ supports multiple types of inheritance. Multiple inheritance in C++ is the ability to have two or more base classes. The benefits for the user would be to create objects, which would have the characteristics of both its parent or base classes.

Single Inheritance

  • In single inheritance, a derived class has only one base class.
  • A simple hierarchy is used, like a straight family tree.
  • One-to-one inheritance of members
  • Specialises in a single base class

Multiple Inheritance

  • A drive class has more than one base class.
  • Creates a more complex hierarchy, like combining two families.
  • The derived class inherits members from all base classes.
  • You can combine multiple classes.
  • Same inheritance and access rules

Multiple forms of inheritance

There are five main types of inheritance in C++:

1. Single Inheritance

In single inheritance, the child class inherits the variable and function from the parent’s class. The child class is known as the derived class, and the parent class is known as the base class. It is also known as simple inheritance.

Single Inheritance
#include <iostream>
using namespace std;

class Animal {
public:
    void eat() {
        cout << "This animal eats food." << endl;
    }
};

class Dog : public Animal {
public:
    void bark() {
        cout << "The dog barks." << endl;
    }
};

int main() {
    Dog d;
    d.eat();
    d.bark();
    return 0;
}

2. Multiple Inheritance

In multiple inheritance, the child class inherits variables and functions from multiple base classes.

Multiple Inheritance
#include <iostream>
using namespace std;

class Animal {
public:
    void eat() {
        cout << "This animal eats food." << endl;
    }
};

class Pet {
public:
    void play() {
        cout << "This pet loves to play." << endl;
    }
};

class Dog : public Animal, public Pet {
public:
    void bark() {
        cout << "The dog barks." << endl;
    }
};

int main() {
    Dog d;
    d.eat();
    d.play();
    d.bark();
    return 0;
}

3. Multilevel Inheritance

In multilevel inheritance, the derived class inherits from another derived class and makes a chain of inheritance.

Multilevel Inheritance
#include <iostream>
using namespace std;

class Animal {
public:
    void eat() {
        cout << "This animal eats food." << endl;
    }
};

class Dog : public Animal {
public:
    void bark() {
        cout << "The dog barks." << endl;
    }
};

class Puppy : public Dog {
public:
    void weep() {
        cout << "The puppy weeps softly." << endl;
    }
};

int main() {
    Puppy p;
    p.eat();
    p.bark(); 
    p.weep();
    return 0;
}

4. Hierarchical Inheritance

In hierarchical inheritance, have a multiple drive class and inherit variables and function for one base class.

Hierarchical Inheritance
#include <iostream>
using namespace std;

class Animal {
public:
    void eat() {
        cout << "This animal eats food." << endl;
    }
};

class Dog : public Animal {
public:
    void bark() {
        cout << "The dog barks." << endl;
    }
};

class Cat : public Animal {
public:
    void meow() {
        cout << "The cat meows." << endl;
    }
};

int main() {
    Dog d;
    d.eat();
    d.bark();

    Cat c;
    c.eat();
    c.meow();

    return 0;
}

5. Hybrid Inheritance

When multiple typesfunctions of inheritance are combined in one program, it is known as hybrid inheritance.

Hybrid Inheritance
#include <iostream>
using namespace std;

class Animal {
public:
    void eat() {
        cout << "This animal eats food." << endl;
    }
};

class Dog : public Animal {
public:
    void bark() {
        cout << "The dog barks." << endl;
    }
};

class Pet {
public:
    void play() {
        cout << "This pet loves to play." << endl;
    }
};

class Puppy : public Dog, public Pet {
public:
    void weep() {
        cout << "The puppy weeps softly." << endl;
    }
};

int main() {
    Puppy p;
    p.eat();  
    p.bark();
    p.play();
    p.weep();
    return 0;
}

Advantages of Inheritance

  • The biggest advantage of inheritance is code reusability.
  • It can improve the code design and make it easy to collaborate.
  • Multiple classes’ code can be used in a different class.
  • It also helps to hide the details of the program which is not used.

Disadvantages of Inheritance

  • The derived class depended on the base class
  • If a lotclass. of classes are inherited, then it can make it difficult for the programmer.
  • If multiple classes are used using inheritance, then the error rectification can become difficult.
  • Constructors and Destructors in Derived Classes

Disclaimer: We have provide you with the accurate handout of β€œInheritance in C++β€œ. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above study material present on our websites is for education purpose, not our copyrights.

Images and content shown above are the property of individual organisations and are used here for reference purposes only. To make it easy to understand, some of the content and images are generated by AI and cross-checked by the teachers.

cbseskilleducation.com

Leave a Comment