write a program to implement multiple inheritance in c++ ?

vijaya shankar.k | 517 Views | c++ | 11 Aug 2016

 

Inheritance :

  • In C++ inheritance can be described as a process of creating new classes from existing classes.
  • New classes inherit some of the properties and behaviour of the existing classes.An existing class that is "parent" of a new class is called a base class. New class that inherits properties of the base class is called a derived class.

Advantages :

  • Using this inheritance technique we can reuse code .
  • It also provides possibility to extend existing classes by creating derived classes.

Syntax :

class DerivedClass : accessSpecifier BaseClass
  • In this syntax an access specifier can be public, protected and private. The default access specifier is private. 
  • Access specifiers affect accessibility of data members of base class from the derived class.
  • In addition, it determines the accessibility of data members of base class outside the derived class.

Types Of Inheritance :

There are different types of inheritance:

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid (Virtual) Inheritance

Multiple Inheritance :

  • Multiple inheritance enables a derived class to inherit members from more than one parent.
  • That Deriving directly from more than one class is usually called multiple inheritance.

Syntax :

Class DerivedClass: accessSpecifier BaseClass1, BaseClass2, …, BaseClassN

Example :

#include<iostream.h>
#include<conio.h>
class child1 //Derived Class1
{
protected:
int a;
public:
void geta() // Get the input value of "a"
{
cout<<"Enter the a value :";
cin>>a;
} };
class child2 // Derived Class2
{
protected:
int b;
public:
void getb() // Get the input value of "b"
{
cout<<"\nEnter the b value:";
cin>>b;
} };
class parent:public child1,public child2 // Call the drived class
{
int c;
public:
void display() // Display the sum value
{
c=(a + b);
cout<<"\nsum : "<<c;
}
void main()
}; {
clrscr();
parent p; // object creation
p.geta();
p.getb();
p.display();
getch();
}

Output :