OOPS - [C++ Multiple Inheritance]

♠ Posted by Unknown in at 03:52

Multiple Inheritance


A class can inherit the attributes of two or more classes known as multiple inheritance. Multiple inheritance allows us to combine the feature 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 another.
Syntax:

            Class D: visibility B-1, visibility B-2 {……………};

Image: Multiple Inheritance

Example:

#include<iostream.h>
#include<conio.h>
class A
{
 protected:
            int a;
 public:
            void get_a()
            {
             cout<<"Enter The Value For A : ";
             cin>>a;
            }
            void put_a()
            {
             cout<<"The Value Of A : "<<a<<endl;
            }
};

class B
{
 protected:
            int b;
 public:
            void get_b()
            {
             cout<<"Enter The Value For B : ";
             cin>>b;
            }
            void put_b()
            {
             cout<<"The Value Of B : "<<b<<endl;
            }
};

class C : public A, public B
{
            int c;
 public:
            void put_c()
            {
             c = a + b;
             cout<<"The Answer Is : "<<c<<endl;
            }
};
void main()
{
 C objc;
 clrscr();
 objc.get_a();
 objc.get_b();
 objc.put_c();
 getch();
}

0 comments:

Post a Comment