C++ Inheritance and It Types

Inheritance is one of the fundamental concepts of Object-Oriented Programming (OOP), and it allows a class to inherit properties and behaviors from another class. This means that a new class can be created that is based on an existing class, and it can inherit all or some of the properties and methods of that class.

C++ supports several types of inheritance, including:

  1. Single Inheritance: This is the simplest form of inheritance, where a single class inherits from a single base class.

  2. Multiple Inheritance: This is a form of inheritance where a single class inherits from multiple base classes.

  3. Multilevel Inheritance: This is a form of inheritance where a class inherits from a base class, which in turn inherits from another base class.

  4. Hierarchical Inheritance: This is a form of inheritance where multiple classes inherit from a single base class.

  5. Hybrid Inheritance: This is a form of inheritance that combines multiple forms of inheritance, such as single, multiple, multilevel, and hierarchical inheritance.

C++ Single Inheritance

C++ Single Inheritance refers to the mechanism in which a derived class inherits the features of a single base class. It is a process in which the derived class acquires the properties and behaviors of the base class.

In other words, the derived class inherits the members of the base class, including its methods, data members, and other members, and can use them as if they were part of its own class. The base class is called the parent class or the superclass, and the derived class is called the child class or the subclass.

Consider the following example of single inheritance in C++:

1#include <iostream> 2#include <string> 3 4class Person 5{ 6 private: 7 std::string name; 8 public: 9 void setName(std::string n) 10 { 11 name = n; 12 } 13 std::string getName() 14 { 15 return name; 16 } 17}; 18 19class Employee: public Person 20{ 21 private: 22 int employeeId; 23 public: 24 void setId(int id) 25 { 26 employeeId = id; 27 } 28 int getId() 29 { 30 return employeeId; 31 } 32}; 33 34int main() 35{ 36 Employee emp1; 37 emp1.setName("William Max"); 38 emp1.setId(12345); 39 std::cout << "Employee Name: " << emp1.getName() << std::endl; 40 std::cout << "Employee ID: " << emp1.getId() << std::endl; 41 return 0; 42}

Output:

Employee Name: William Max Employee ID: 12345

In this example, the "Employee" class inherits from the "Person" class. The "Employee" class has access to all the public members of the "Person" class, including the "name" member and the "setName" and "getName" methods. The "Employee" class also has its own private members and methods, such as the "employeeId" member and the "setId" and "getId" methods.

C++ Multiple Inheritance

Multiple Inheritance in C++ is a feature that allows a class to inherit members and properties from multiple parent classes. This allows a derived class to inherit characteristics of multiple base classes and avoid code duplication.

To understand multiple inheritance, consider a scenario where you have a class called “Employee” and another class called “Person”. The Employee class contains the details about an employee such as name, salary, and employee id, while the Person class contains the details about a person such as name, age, and address. Now, if you want to create a class that contains both the employee and personal details, you can create a derived class from both Employee and Person classes.

Here is an example to demonstrate multiple inheritance in C++:

1#include<iostream> 2using namespace std; 3 4// Base class - Employee 5class Employee 6{ 7 public: 8 int empId; 9 string empName; 10 float salary; 11}; 12 13// Base class - Person 14class Person 15{ 16 public: 17 string name; 18 int age; 19 string address; 20}; 21 22// Derived class - Employee_Person, inherits from Employee and Person classes 23class Employee_Person: public Employee, public Person 24{ 25}; 26 27int main() 28{ 29 // Declaring object of derived class 30 Employee_Person obj; 31 obj.empId = 1001; 32 obj.empName = "William"; 33 obj.salary = 50000; 34 obj.name = "William Max"; 35 obj.age = 30; 36 obj.address = "XYZ"; 37 38 // Printing the values 39 cout << "Employee Id: " << obj.empId << endl; 40 cout << "Employee Name: " << obj.empName << endl; 41 cout << "Salary: " << obj.salary << endl; 42 cout << "Person Name: " << obj.name << endl; 43 cout << "Age: " << obj.age << endl; 44 cout << "Address: " << obj.address << endl; 45 return 0; 46}

Output:

1Employee Id: 1001 2Employee Name: William 3Salary: 50000 4Person Name: William Max 5Age: 30 6Address: XYZ

In this example, the derived class Employee_Person inherits the properties and members of both Employee and Person classes. This allows the objects of the derived class to access all the properties and members of both the parent classes.

Multiple inheritance can become complex when two base classes contain the same member names. To handle such situations, C++ provides the scope resolution operator (::) to access the members of specific classes.

C++ Multilevel Inheritance in C++

Multilevel inheritance is a concept in object-oriented programming that refers to a class deriving from a derived class. This type of inheritance allows for a hierarchy of inheritance to be established, with a base class passing its characteristics to a derived class, which in turn passes its own characteristics to another derived class.

In C++, multilevel inheritance can be implemented by having a derived class inherit from another derived class. This creates a relationship where the derived class becomes the base class for another derived class.

Let's consider an example to understand the concept of multilevel inheritance in C++:

1#include <iostream> 2using namespace std; 3 4// Base class 5class Animal { 6public: 7 void eat() { 8 cout << "Animal is eating" << endl; 9 } 10}; 11 12// Derived class 1 13class Mammal : public Animal { 14public: 15 void walk() { 16 cout << "Mammal is walking" << endl; 17 } 18}; 19 20// Derived class 2 21class Dog : public Mammal { 22public: 23 void bark() { 24 cout << "Dog is barking" << endl; 25 } 26}; 27 28int main() { 29 Dog d; 30 d.eat(); 31 d.walk(); 32 d.bark(); 33 return 0; 34}

The output of the above program will be:

1Animal is eating 2Mammal is walking 3Dog is barking

In this example, we have a base class named Animal which has a function eat(). Then, we have a derived class named Mammal which inherits from the Animal class and also has its own function walk(). Finally, we have another derived class named Dog which inherits from the Mammal class and has its own function bark().

In the main function, we create an object of the Dog class and call its functions eat(), walk(), and bark(). The Dog class inherits the functions eat() and walk() from the Animal and Mammal classes respectively.

In this way, multilevel inheritance allows a class to inherit the characteristics of its ancestors in a hierarchical manner, making it a powerful tool in object-oriented programming.

C++ Hierarchical Inheritance

C++ Hierarchical Inheritance refers to the inheritance relationship in which one class serves as the base class for multiple subclasses. In this type of inheritance, multiple derived classes inherit properties and behaviors from a single base class. This provides a way to categorize objects into a hierarchy based on their characteristics and behaviors.

Here is an example to illustrate C++ Hierarchical Inheritance:

1#include <iostream> 2using namespace std; 3 4class Shape { 5 public: 6 void setWidth(int w) { 7 width = w; 8 } 9 void setHeight(int h) { 10 height = h; 11 } 12 protected: 13 int width; 14 int height; 15}; 16 17class Rectangle: public Shape { 18 public: 19 int getArea() { 20 return (width * height); 21 } 22}; 23 24class Triangle: public Shape { 25 public: 26 int getArea() { 27 return (width * height)/2; 28 } 29}; 30 31int main(void) { 32 Rectangle rect; 33 Triangle tri; 34 35 rect.setWidth(5); 36 rect.setHeight(7); 37 38 tri.setWidth(5); 39 tri.setHeight(7); 40 41 cout << "Rectangle area: " << rect.getArea() << endl; 42 cout << "Triangle area: " << tri.getArea() << endl; 43 44 return 0; 45}

In the example above, we have defined a base class Shape with two member functions setWidth and setHeight to set the width and height of the shape. We have also defined two derived classes Rectangle and Triangle which inherit from the base class Shape. The derived classes have their own member function getArea which calculates and returns the area of the respective shape.

The output of this program will be:

1Rectangle area: 35 2Triangle area: 17.5

In this example, the Rectangle and Triangle classes both inherit the width and height properties from the Shape class. This type of inheritance allows us to define common properties in the base class and then reuse them in the derived classes. It makes our code more organized and maintainable.

C++ Hybrid Inheritance

C++ Hybrid Inheritance is a combination of two or more inheritance types in a single inheritance hierarchy. It is a combination of multiple inheritance, multilevel inheritance and hierarchical inheritance.

Multiple inheritance refers to a situation where a derived class inherits from more than one base class. Multilevel inheritance refers to a situation where a derived class inherits from a base class which in turn inherits from another base class. Hierarchical inheritance refers to a situation where a number of derived classes inherit from a single base class.

A real-life example of hybrid inheritance can be a student who has a scholarship for his/her education. This student can inherit the properties of a student as well as a scholarship holder. This example shows the combination of multiple inheritance and hierarchical inheritance.

Here is an example to demonstrate the concept of hybrid inheritance in C++:

1#include <iostream> 2using namespace std; 3 4class A 5{ 6public: 7 int x; 8}; 9 10class B : public A 11{ 12public: 13 int y; 14}; 15 16class C : public A 17{ 18public: 19 int z; 20}; 21 22class D : public B, public C 23{ 24public: 25 int sum; 26}; 27 28int main() { 29 D obj; 30 obj.x = 25; 31 obj.y = 50; 32 obj.z = 75; 33 obj.sum = obj.x + obj.y + obj.z; 34 cout << "sum: " << obj.sum << endl; 35 return 0; 36}

In this example, class A is the base class and classes B and C are derived from class A, forming a hierarchical inheritance. Class D is derived from both class B and class C, forming a multiple inheritance. The sum variable in class D is calculated by adding the values of x, y, and z from classes A, B, and C respectively.

The output of this program would be: sum: 150

This example shows how hybrid inheritance can be used to combine the properties and behaviors of different base classes into a single derived class.