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++.






Single Inheritance in c++

In this tutorial we learn the concept of Inheritance in c++.
    Inheritance is the most powerful feature in the c++ language that allows us to the code re usability. That is better for every programmer when we are required the data again and again  then instead of writing that code again use inheritance. This feature save the time as well as money and also increases the reliability.

Que:What is mean by Inheritance?
Ans: The inheritance is the creating a new class form already exiting class. The old or existing class is known as Base class (super class or parent class) and the new class is known as Derived class(child class or sub class.

Types of Inheritance:


1)Single Inheritance

2)Multiple Inheritance
 
3)Multilevel Inheritance
4)Hierarchical Inheritance
 
5)Hybrid Inheritance


Here we learn first Single Inheritance:
 This type of Inheritance is done when the new class is creating only a one Base class. The Inheritance feature is allows us to use the data of the parent class.



Syntax:
 
 Class base-Class
{
                …………………
                ………………..
};
Class derived-class:base-class
                …………………….;
                ……………………;
}

The following code shows the single inheritance of student and teacher class in this code Teacher is the super class and the Student is the drived class. The Student class is uses the feature of the Teacher class.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Teacher
{
                char name[20];
                public:
                                char sub[20];
                                void getdata()
                                {
                                                cout<<"\nFor Teacher:";
                                                cout<<"\nEnter Teacher Name:";
                                                gets(name);
                                                cout<<"\nEnter subject:";
                                                gets(sub);
                                }
                                void display()
                                {
                                                cout<<"\n*****Teacher Information*****";
                                                cout<<"\nName:"<<name;
                                                cout<<"\nSubject:"<<sub;
                                }
};
class Student:public Teacher
{
                char sname[20],cls[20];
                int rno;
                public:
                                void getinfo()
                                {
                                                getdata();
                                                cout<<"\nFor Student:";
                                                cout<<"\nEnter Student Name:";
                                                gets(sname);
                                                cout<<"\nEnter class of Student:";
                                                gets(cls);
                                                cout<<"\nEnter roll no of Student:";
                                                cin>>rno;
                                }
                                void show()
                                {
                                                display();
                                                cout<<"\n*****Student Information*****";
                                                cout<<"\nName:"<<sname;
                                                cout<<"\nClass:"<<cls;
                                                cout<<"\nRoll No.:"<<rno;
                                }
};
void main()
{
                Student s1;
                clrscr();
                s1.getinfo();
                s1.show();
                getch();
}

The following code shows the OUTPUT:


 You may also like:

Multiple Inheritance in C++.

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...