Multiple Inheritance



Multiple Inheritance:  The inheritance in which the multiple parent class properties or member functions are inherited or acquired by  single child class.In simple word,only one child class is derived or obtained from more than one parent class.

syntax:  class ParentOne

                {

                        code related to ParentOne;

                                    };

            class ParentTwo

                       {

                                     code related to Parent Two;

                                          }

            class Child : visibility mode ParentOne,visibility mode ParentTwo  

               {

                         code related to child 

                                    } ;

Example: 

1. WAP to define a class Student and AcademicMarks which have data

 members to represent Studnet and AcademicMarks respectively. Define

 member function to initialize marks in each class.Derive a class  Result resolved from

 Student and AcademicMarks  and calculate total marks and display result.

i.Using public visibility mode

 #include<iostream.h>

#include<conio.h>

class Student

  {

    protected: char name[20];

               int roll;

    public: void inputS();

            void displayS();

                 };

     void Student::inputS()

       {

        cout<<"Enter name:";

        cin>>name;

        cout<<"Enter roll number:";

        cin>>roll;

          }

       void Student::displayS()

        {

         cout<<"Name:"<<name<<endl<<"Roll no:"<<roll<<endl;

           }

           //Another class called AcademicMarks

     class AcademicMarks

     {

   protected: int math;

              int physics;

              int Computer;

   public : void inputAca();

            void displayAca();

                   };

    void AcademicMarks::inputAca()

       {

        cout<<"Enter marks obtained in Math:";

        cin>>math;

        cout<<"Enter marks obtained in Physic:";

        cin>>physics;

        cout<<"Enter marks obtained in Computer:";

        cin>>Computer;

          }

    void AcademicMarks::displayAca()

      {

       cout<<"Math:"<<math<<endl<<"Physic:"<<physics<<endl<<"Computer:"<<Computer;

         }

class Result: public Student,public AcademicMarks

  {

    int total;

  public: void calculate();

            };

    void Result::calculate()

     {

      total=math+physics+Computer;

      cout<<"Total marks :"<<total;

         }

      void main()

      {

         Result r;

         r.inputS();

         r.inputAca();

         cout<<endl;

         r.displayS();

         r.displayAca();

         getch();

           }



iii.Using private visibility mode

#include<iostream.h>

#include<conio.h>


class Student

  {

  protected: char name[20];

               int roll;

    public: void inputS();

            void displayS();

                 };

     void Student::inputS()

       {

        cout<<"Enter name:";

        cin>>name;

        cout<<"Enter roll number:";

        cin>>roll;

          }

       void Student::displayS()

        {

         cout<<"Name:"<<name<<endl<<"Roll no:"<<roll<<endl;

           }

           //Another class called AcademicMarks

     class AcademicMarks

     {

   protected: int math;

              int physics;

              int Computer;

   public : void inputAca();

            void displayAca();

                   };

    void AcademicMarks::inputAca()

       {

        cout<<"Enter marks obtained in Math:";

        cin>>math;

        cout<<"Enter marks obtained in Physic:";

        cin>>physics;

        cout<<"Enter marks obtained in Computer:";

        cin>>Computer;

          }

    void AcademicMarks::displayAca()

      {

       cout<<"Math:"<<math<<endl<<"Physic:"<<physics<<endl<<"Computer:"<<Computer<<endl;

         }

 class Result: private Student,private AcademicMarks

  {

    int total;

  public: void calculate();

            };

    void Result::calculate()

     {

     Student::inputS();

     AcademicMarks::inputAca();

     Student::displayS();

     AcademicMarks::displayAca();

      total=math+physics+Computer;

      cout<<"Total marks :"<<total;

         }

      void main()

      {

          Result r;

         r.calculate();

         getch();

           }

Output



Comments