My Blog List

Thursday, January 2, 2020

Multiple Inheritance in C++.

     In this tutorial Multiple Inheritance in c++.
On the basis of Inheritance concept the another type of inheritance is the Multiple Inheritance. 

  This type inheritance use to create a new class from multiple base class.In this technique two or more class use to create new class.
By this you can use the code or data from the different class in derived class.

Syntax:

    class base-class1
    {
        ............;
        ............;
    }
    class base-class2
    {
        ...........;
        ...........;
    }
    class derived-class:base-class1,base-class2
    {
        ...........;
        ...........;
    } 

 Suppose the student is the derived class.And the Teacher and subject is the two different base.Then the Student class is derived form other two base class is known as Multiple inheritance.

Here following code shows the multiple inheritance.


#include<iostream.h>
#include<conio.h>
class area
{
    public:
        int area;
        void cal_area(int l,int b);
};
class perimeter
{
    public:
        int peri;
        void cal_peri(int l,int b);
};
class rectangle:public area,public perimeter
{
    public:
        void display();
};
void area::cal_area(int length, int breath)
{
    area=length*breath;
}
void perimeter::cal_peri(int length,int breath)
{
    peri=2*length+2*breath;
}
void rectangle::display()
{
    cout<<"\nArea="<<area<<"\nPerimeter="<<peri;
}
void main()
{
    rectangle r;
    int l,b;
    clrscr();
    cout<<"\nEnter length & breath:";
    cin>>l>>b;
    r.cal_area(l,b);
    r.cal_peri(l,b);
    r.display();
    getch();
}

Output:

You May Also Like:

Single Inheritance in c++.






1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Multiple Inheritance in C++.

     In this tutorial Multiple Inheritance in c++. On the basis of Inheritance concept the another type of inheritance is the Multiple I...