Multiple inheritances in C++

 Multiple inheritances:

  • Multiple inheritances have a number of base classes and one derived class.
  • It means the numbers of base classes are derived from one derived class.

Syntax:

  1. class a

    {

              //Base class1

    };

    class b

    {

              //Base class2

    };

    class c:public a,public b

    {

              //Derived class

    };

    //Object creation

    Derived_class_name object_name;

    object_name.function_name();

Example:

  1. #include<iostream.h>

    #include<conio.h>

    class first //Base class1

    {

    public:

    int num1;

    void get_1st_value()

    {

    cout<<"\n Enter first value: ";

    cin>>num1;

    }

    };

    class second //Base class2

    {

    public:

    int num2;

    void get_2nd_value()

    {

    cout<<"\n Enter Second value: ";

    cin>>num2;

    }

    };

    class add: public first, public second //Derived class

    {

    public:

    int num3;

    void operation()

    {

    num3=num1+num2;

    }

    void display()

    {

    cout<<"\n \n Output:";

    cout<<"\n \n First number = "<<num1;

    cout<<"\n \n Second number = "<<num2;

    cout<<"\n \n First number + Second number =  "<<num3;

    }

    };

    void main()

    {

    clrscr();

    add a;

    a.get_1st_value();

    a.get_2nd_value();

    a.operation();

    a.display();

    getch();

    }

Output:

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me