In some situations, it is essential to design a program using two or more forms of Inheritance. Combining two or more forms of Inheritance to design a program is known as Hybrid Inheritance in C++. Such a form of Inheritance represents a complex class hierarchy. The following figures show some possible representations of hybrid inheritance.
The above Figure A represents hybrid Inheritance as it is a combination of multilevel and multiple Inheritance. Similarly, figure B represents hybrid Inheritance as it is a combination of multiple Inheritance and multilevel Inheritance with a different structure.
To understand hybrid Inheritance, consider an example of calculating a student’s result based on marks obtained in internal and external examinations. To simulate this problem, the various classes involved and their inheritance relationships are described in Figure, which indicates both multilevel and multiple Inheritance.
The student class maintains basic information of a student like rollno and name. The class exam_internal derived from student class that inherits students’ information and maintains marks obtained in the internal examination. The class exam_external maintain marks obtained in his external examination. We assume that the maximum marks in a subject for internal and external examination are 40 and 60, respectively. (So maximum marks in a subject is 100). The resulting class derived from the classes exam_internal and exam_external that calculates the result of a student. The above hybrid inheritance implements in the following program.
#include<iostream.h> #include<conio.h> class student { private: int rollno; char name[20]; public: void read() { cout<<"\nEnter rollno and name :"; cin>>rollno>>name; } void show() { cout<<"\nRoll no = "<<rollno; cout<<"\nName = "<<name; } }; class exam_internal : public student { protected: int sub1_marks; int sub2_marks; public: void read_marks() { cout<<"Enter Internal marks of subject 1(MAX 40) "; cin>>sub1_marks; cout<<"Enter Internal marks of subject 2(MAX 40) "; cin>>sub2_marks; } void display_marks() { cout<<"\nInternal marks of subject 1 = "<<sub1_marks; cout<<"\nInternal marks of subject 2 = "<<sub2_marks; } }; class exam_external { protected: int sub1_ext_marks; int sub2_ext_marks; public: void read_marks() { cout<<"Enter external marks of subject 1 = "; cin>>sub1_ext_marks; cout<<"Enter external marks of subject 2 = "; cin>>sub2_ext_marks; } void display_marks() { cout<<"\nExternal marks of subject 1 "<<sub1_ext_marks; cout<<"\nExternal marks of subject 2 "<<sub2_ext_marks; } }; class result:public exam_internal, public exam_external { private: int total_marks; public: void cal_result() { total_marks=sub1_marks + sub2_marks + sub1_ext_marks + sub2_ext_marks; cout<<"Total mark obtained = "<<total_marks; } }; int main() { clrscr(); result r1; cout<<"Enter student information : \n"; r1.read(); //call read() of student class cout<<"Enter marks of Internal Examination :\n"; r1.exam_internal::read_marks(); cout<<"\nEnter marks of External Examination : \n"; r1.exam_external::read_marks(); cout<<"Displaying Student Details \n"; r1.show(); //calls show() of student class r1.exam_internal::display_marks(); r1.exam_external::display_marks(); cout<<"\ncalculating and displaying result\n"; r1.cal_result();//call cal_result of result class getch(); return 0; }
Explanation: In main(), we create the object r of the result class through which we input the student’s basic information, his internal and external marks details. Then, the inputted information is displayed—finally, the statement.
r1.cal_result();
invokes the cal_result ()of result class that calculated the total marks obtained by a student, displayed.