CPP Program for Implementing the Multiple Inheritance Concept

Aim:

To write a C++ program for implementing the multiple inheritance concept.

Inheritance in C++

  • Inheritance is the capability of one class to acquire properties and characteristics from another class.
  • The class whose properties are inherited by another class is called the Parent or Base or Superclass.
  • And, the class which inherits properties of other class is called Child or Derived or Sub class.
  • Inheritance makes the code reusable.
  • When we inherit an existing class, all its methods and fields become available in the new class, hence code is reused.

NOTE: All members of a class except Private, are inherited


Purpose of Inheritance in C++

  1. Code Reusability
  2. Method Overriding (Hence, Runtime Polymorphism.)
  3. Use of Virtual Keyword

Access Control and Inheritance

  • A derived class can access all the non-private members of its base class.
  • Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

We can summarize the different access types according to – who can access them in the following way −

Accesspublicprotectedprivate
Same classyesyesyes
Derived classesyesyesno
Outside classesyesnono

A derived class inherits all base class methods with the following exceptions −

  • Constructors, destructors and copy constructors of the base class.
  • Overloaded operators of the base class.
  • The friend functions of the base class.

Basic Syntax of Inheritance:

class Subclass_name : access_mode Superclass_name
  • While defining a subclass like this, the super class must be already defined or atleast declared before the subclass declaration.
  • Access Mode is used to specify, the mode in which the properties of superclass will be inherited into subclass, public, privtate or protected.

Access Modifiers and Inheritance: Visibility of Class Members

  • Depending on the Access modifier used while inheritance, the availability of class members of Superclass in the subclass changes.
  • It can either be private, protected, or public.

1) Public Inheritance

  • This is the most used inheritance mode.
  • In this the protected member of super class becomes protected members of sub class and public becomes public.
class Subclass : public Superclass

2) Private Inheritance

  • In private mode, the protected and public members of super class become private members of derived class.
class Subclass : Superclass   // By default its private inheritance

3) Protected Inheritance

  • In protected mode, the public and protected members of Super class becomes protected members of Sub class.
class subclass : protected Superclass

Types of Inheritance in C++

In C++, we have 5 different types of Inheritance. Namely,

  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance (also known as Virtual Inheritance)

Multiple Inheritance in C++

In this type of inheritance a single derived class may inherit from two or more than two base classes.

Algorithm:

  1. Start the program.
  2. Declare the base class student.
  3. Declare and define the function get() to get the student details.
  4. Declare the other class sports.
  5. Declare and define the function getsm() to read the sports mark.
  6. Create the class statement derived from student and sports.
  7. Declare and define the function display() to find out the total and average.
  8. Declare the derived class object,call the functions get(),getsm() and display().
  9. Stop the program.

Program:

#include<iostream> 
using namespace std;
class A
{
protected:
int x;
public:

void getx()
{
cout << "enter value of x: "; cin >> x;cout<<x;
}
};
class B
{
protected:
int y;
public:
void gety()
{
cout << "\nenter value of y: "; cin >> y;cout<<y;
}
};
class C : public A, public B   
{
public:
void sum()
{
cout << "\nSum = " << x+ y;
}
};
int main()
{
C obj1; 
obj1.getx();
obj1.gety();
obj1.sum();
return 0;
}  

Execution:

Input
33 42

Output
enter value of x: 33
enter value of y: 42
Sum = 75

Result:

Thus, the Implement the Multiple Inheritance Concept was executed successfully.

Leave a Comment