Multiple Inheritance And Its C++ Program - Coding Bot

Like Us On Facebook

Follow Us On Twitter

Drop Down Menu

Monday, 24 June 2013

Multiple Inheritance And Its C++ Program

Hi Guys,
Today we are going to discuss about the last type of Inheritance we have seen till yet, Multiple Inheritance.
Have you learnt about Inheritance  and its different types? Check Out Now:

There are 5 types of Inheritances used in C++:
1) Single Inheritance
2) Multilevel Inheritance
3) Hierarchial Inheritance
4) Hybrid Inheritance
5) Multiple Inheritance

A class can inherit the attributes of two or more classes as shown in fig. A. This is known as Multiple Inheritance. Multiple Inheritance allows us to combine the features of several existing classes as a starting point for defining new classes. It is like a child inheriting the physical features of one parent and the intelligence of the another.

Multiple Inheritance And Its C++ Program
fig. (A)
The syntax of a derived class with multiple base classes is as follows:

class D: visibility B1visibility B1...
{
          ..........
          .......... (Body Of D)
          ..........
};

where, visibility modes may be either public or private. The base classes are separated by commas.

Example:

   class P : public M, public N
   {
        public:
            void display(void);
   };

The following program shows the program code illustrating how all the three classes are implemented in Multiple Inheritance mode.

Multiple Inheritance C++ Program:

/* Double-Click To Select Code */
#include<iostream.h>
#include<conio.h>
class M
{
 protected:
  int m;
 public:
  void get_m(int x)
  {
   m = x;
  }
};

class N
{
 protected:
  int n;
 public:
  void get_n(int y)
  {
   n = y;
  }
};

class P : public M, public N
{
 public:
  void display()
  {
   cout<<"M = "<<m<<"\n\n";
   cout<<"N = "<<n<<"\n\n";
   cout<<"M*N = "<<m*n<<"\n";
  }
};

void main()
{
 clrscr();
 P p;
 p.get_m(10);
 p.get_n(20);
 p.display();
 getch();
}


Program Output:

Multiple Inheritance And Its C++ Program

Please Comment If You Liked The Post.


Do you like this post? Please link back to this article by copying one of the codes below.

URL: HTML link code: Forum link code: