12. CPP-Multiple Inheritance

CPP Inheritance: Previous                                                   CPP Hierarchical Inheritance

When sub class takes features of more than one base class, then it is called as multiple inheritance.

Eg: Final exam takes marks from theory exams as well as practical exams also. So to calculate the final exam’s result we need both results[theory exam, practical exam]
Syntax :
: ,
             {
                  //body of subclass;
            }
Eg:
class Theory_Exam
{
int first, second_test;
                        protected:
Theory_Exam();
                                       {
                                                cout<<”enter total of first test marks  =“;
                                                cin>>first;
                                                cout<<”enter total of second test marks=”;
                                                cin>>second_test;
                                        }
                                        void display();
                                       {
                                                 cout<<”first test total is=”<
                                                 cout<<”second test total is=”<
                                        }                                       
                                                         };
                                                     class  Practical_Exam
                                                     {
                                                                  intp_exam;
                                                                protected:
                                                                                Practical_Exam()
                                                                                {
                                                                                                    cout<<”Enter practical exam total= “:
                        cin>>p_exam;                     
                                                                               }
                                                                             void display()
                                                                             {
                                                                                                 cout<<”marks of practicals exams is =”<
                                                                              }
                                                };
                                              class Final_Exam : protected Theory_Exam, protected Practical_Exam
                                              {
                                                               private:
                                                                                int marks[3];
                                                            public:
                                                                              Final_Exam();
                                                                            {
                                                                                   int total=0;
                                                                                    int i;
                                                                                       for(i=0;i<3 i="" span="">
                                                                                       {
                                                                                                           cout<<”Enter marks = “;
                                                                                                           cin>>marks[i];
                                                                                                           total=total+marks[i];
                                                                                       }
                                                                           }
                                                                            void display()
                                                                            {
                                                                                                        Theory_Exam::display();
                                                                                  
                       Practical_Exam::display();
                                                                                                          cout<<<”Total of final exam is= “<
                                                                            }
                                             };
                                          void main()
                                         {
                                                           Final_Exam f;
                                                            f.display();
                                       }
Multilevel inheritance :
If we have many levels in inheritance process, in which one base class becomes derived class for another next level and so on. Eg:
#include
#define SIZE 25;
class Person
{
                   private:
                           char name[SIZE];
int age;
                   protected:
                                                                                    Person();
                                                                                  void display();
                                                           };                                         
                                                         Person::person()
                                                         {
                                                                   cout<<”enter name = “;
                                                                   cin.getline(name,SIZE);
                                                                   cout<<”enter your age= “;
                                                                  cin>>age;                                                             
                                                          }
                                                         void Person::display()
                                                         {
                                                                     cout<<”name is= “<
                                                                     cout<<”age is=”<
                                                          }
class Employee: protected  Person
{
             private :
                       float salary;
                      int   deptno;
            protected:
                     Employee()
                    {
                              cout<<”Enter salary = “;
                              cin>>salary;
                              cout<<”enter deptno = “ ;
                               cin>>deptno;
                     }
                   void display()
                  {
                           Person::display();
                            cout<<”salary of employes is = “ <
                            cout<<”department number is =” <
                }
};
class Manager :protected Employee
{
                private:
                            char deptname[SIZE];
                            Manager()
                           {
                                         cout<<”enter department name =”;
                                          cin.getLine(deptname,SIZE);
                          }
                          void display()
                         {
                                           Employee::display();
                                          cout<<”department name is =”<
                          }
};
void main()
{
                  Manager m;
                    m.display();
}

CPP Inheritance: Previous                                                   CPP Hierarchical Inheritance