Multiple Inheritance
Within the original implementation of C++ language, a derived class could inherit from only one base class. Even along with this restriction, an object-oriented paradigm is a flexible and powerful programming tool. The latest version of the C++ compiler implements the multiple inheritances. Within this chapter, how a class could be derived from more than one base class is explained.
A Multiple inheritances are the process of creating a new class from more than one base class. Syntax for multiple inheritances is same to that of single inheritance.
Class baseA
{
..................
..................
};
class baseB
{
................
................
};
class derivedC:public baseA, public baseB
{
..................
..................
};
A class derivedC is derived from both classes baseA and baseB.
A Multiple inheritance is a derived class that is declared to inherit properties of two or more parent classes (base classes). A Multiple inheritances could merge the behaviour of multiple base classes in a single derived class. A Multiple inheritances have several benefits over the single inheritance like as rich semantics and the ability to directly express complex structures. Within C++language, derived classes has to be declared in during the compilation of all possible combinations of derivations and the program can selected the appropriate class at run time and create object for the application.
Within single inheritance, a derived class has a single base class. Within multiple inheritances, a derived class has multiple base classes. Within a single inheritance hierarchy, a derived class classically represents a specialization of its base class. Within a multiple inheritance hierarchy, a derived class classically represents a combination of its base classes. A rule of inheritance and access do not modify from a single to a multiple inheritance hierarchy. A derived class inherits the data members and methods from all its base classes, regardless of whether the inheritance connects are protected, private or public.
Example Program:
The given program describes how a multiple inheritance could be declared and defined in a program. Given program consists of two base classes and one derived class. The base class basic_info holds the data members:name, rollnumber, sex. An another base class academic_fit holds the data members:course,semester and rank. The derived class financial_assit holds the data member amount besides the data members of the base classes. A derived class has been declared as public inheritance. A member functions are to be used for get information of the derived class from the keyboard and show the contents of class objects on the screen.
#include <iostream.h>
#include <iomanip.h>
class basic_info
{
private:
char name[20];
long int rollno;
char sex;
public:
void getdata();
void display();
};
class academic_fit
{
private :
char course[20];
char semester[10];
int rank;
public:
};
void getdata();
void display();
class financial_assit: private academic_fit, private basic_info
{
private:
float amount;
public:
};
void getdata();
void display();
void basic_info::getdata()
{
cout<<"Enter a name? \n";
cin>>name;
cout<<"Roll no ?\n";
cin>>rollno;
cout<<"Sex ?\n";
cin>>sex;
}
void basic_info::display()
{
cout<<Name<<" ";;
cout<<rollno<<" ";
cout<<sex<<" ";
}
void academic_fit::getdata()
{
cout<<"Course name (B.Tech/MCA/DCA etc) ?\n";
cin>>course;
cout<<"Semester (First/second etc) ? \n";
cin>>semester;
cout<<"Rank of the student\n";
cin>>rank;
}
void academic_fit::display()
{
cout<<course<<" ";
cout<<semester<<" ";
cout<<rank<<" ";
}
void financial_assit::getdata()
{
basic_info::getdata();
academic_fit::getdata();
cout<<"amount in rupees ? \n";
cin>>amount;
}
void financial_assit::display()
{
basic_info::display();
academic_fit::display();
cout<<setprecision(2);
cout<<amount<<" ";
}
void main()
{
financial_assit f;
cout<<"Enter the information for ";
cout<<"Financial assistance\n";
f.getdata();
cout<<endl;
cout<<"Academic performance for financial Assistance\n";
cout<<"----------------------------------------------\n";
cout<<"Name Rollno Sex Course Semester Rank Amount\n";
cout<<"------------------------------------------------\n";
f.display();
cout<<endl;
cout<<"----------------------------------------------\n";
}