Categories: Coding & Developer

Different Types of inheritance in C++

Inheritance is a key feature of object-oriented programming that allows a new class to be based on an existing class. In C++, a class can inherit the properties and behaviors of another class, which is called the base class or parent class, through the process of inheritance. The class that inherits from the base class is called the derived class or child class.
To define a derived class in C++, we use the following syntax:
class DerivedClass : AccessSpecifier BaseClass {
// class members
};

Learn C# Basic

Last Updated: 2022-04-21
4.6 (72329 views)

C# is one of the most and famous programming languages for developing web,windows applications and also video games.

The AccessSpecifier can be public, protected, or private, depending on the level of access the derived class should have to the members of the base class. Here are the rules for accessing base class members in each case:

  • public: Public members of the base class can be accessed by the derived class using the dot operator.
  • protected: Public and protected members of the base class are accessible in the derived class as protected members.
  • private: Public and protected members of the base class are accessible in the derived class as private members.

Here’s an example:
class Animal {
public:
string name;
void eat() {
cout << “The animal is eating.” << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << “The dog is barking.” << endl;
}
};
int main() {
Dog d;
d.name = “Rufus”;
d.eat();
d.bark();
return 0;
}

In this example, we define a Dog class that is derived from the Animal class. The Dog class inherits the public members of the Animal class, which includes the name attribute and the eat() method. We also define a bark() method in the Dog class that is specific to dogs.
In the main() function, we create an instance of the Dog class and set its name to “Rufus”. We then call the eat() method, which is inherited from the Animal class, and the bark() method, which is specific to the Dog class.
Inheritance is a powerful feature of C++ that allows code reuse and promotes a hierarchical organization of classes. It is widely used in software development for creating complex systems with multiple layers of abstraction.

In C++, there are five types of inheritance:

  1. Single Inheritance: In single inheritance, a derived class is derived from only one base class. The derived class inherits all the members (public and protected) of the base class.
  2. Multiple Inheritance: In multiple inheritances, a derived class is derived from two or more base classes. The derived class inherits all the members (public and protected) of all the base classes.
  3. Multilevel Inheritance: In multilevel inheritance, a derived class is derived from another derived class. The derived class inherits all the members (public and protected) of the base class and the intermediate derived class.
  4. Hierarchical Inheritance: In hierarchical inheritance, multiple derived classes are derived from a single base class. Each derived class inherits all the members (public and protected) of the base class.
  5. Hybrid Inheritance: Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance. In hybrid inheritance, a derived class is derived from two or more base classes and also from another derived class.
Top Courses in Coding & Developer
Ashli Varghese

A recent graduate in Political Science from Lady Shri Ram College for Women, Ashli is a published researcher and a poet. She is currently working as a technical writer at Learnfly. She is a huge admirer of British thespian, American sitcoms and crime thrillers in languages with subtitles. She loves exploring art in all forms and is equipped with learning coding and video-editing skills, in her free time.