Multiple Inheritance in C++

Deriving directly from more than one class is usually called multiple inheritance. The example contains a parent class that is called calculator and they have two methods add() and subtract(). The Parent class has two more child classes which are further inherited.

Source Code

#include <iostream>
using namespace std;

class calculator
{
 public:
 void add()
 {
 cout<<"Add method \n\n ";
 }
 void subtract()
 {
 cout<<"\n Subtract Method \n\n ";
 }
 };
class add : public calculator                //add class inherited calculator
{
};
class Subtract : public add                 //subtract class inherited add and further the caluclulotr
{//subtract class be can derived from a derived class which is known as multilevel inhertiance
};
int main()
{
Subtract s;
add a;
cout<<"\n\n\n\t ********** MULTILEVEL INHERITANCE ********** \n\n\n";
a.subtract();
s.subtract();
return 0;
}

Output of the Program

Multiple inheritance in C++

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment