When a class is derived from two unrelated base classes then this form of inheritance is called multiple inheritance. This form of inheritance is implemented by using at least by using three classes. The two classes are base class but they are not inherit to each others and the remaining one is derived classes which are inherit to two base classes.
SYNTAX:
class base_class_name1
{
private:
…………….
public:
…………….
protected:
…………….
};
class base_class_name2
{
private:
……………
public:
……………
protected:
……………
};
class derived_class_name : public base_class_name1 , public base_class_name2
{
private:
…………….
public:
…………….
protected:
…………….
};
Program:
/*PROGRAM TO IMPLEMENT MULTIPLE INHERITANCE BY USING PRIVATE AND PROTECTED VISIBILITY MODE*/
#include<conio.h>
#include<iostream.h>
class base1
{
char name[15];
int age;
public:
base1()
{
cout<<“Enter name: “;
cin>>name;
cout<<“Enter age: “;
cin>>age;
}
void show1()
{
cout<<“Name is: “<<name<<endl;
cout<<“age is: “<<age<<endl;
}
~base1()
{
cout<<“Destructor of first base class is
executed”<<endl;
}
};
class base2
{
float salary;
char des[5];
public:
base2()
{
cout<<“Enter salary: “;
cin>>salary;
cout<<“Enter designation: “;
cin>>des;
}
void show2()
{
cout<<“Salary is: “<<salary<<endl;
cout<<“Designation is: “<<des<<endl;
}
~base2()
{
cout<<“Destructor of second base class is
executed”<<endl;
}
};
class derived:private base1,protected base2
{
public:
derived():base1(),base2()
{
}
void show3()
{
show1();
show2();
}
~derived()
{
cout<<“Destructor of derived class is executed”<<endl;
}
};
void main()
{
clrscr();
{
derived d;
d.show3();
}
getch();
}
Ambiguity in multiple inheritance