Thursday, March 14, 2013

Multipath Inheritance – C++.



When a class is derived from two or more classes, which are derived from the same base class is know as multipath inheritance. Multipath inheritance can consists many types like single, multiple, multilevel and hierarchical etc.


Example : class Marks and Extra_curricular derived from Student class and class Result drived from both Marks and Extra_curricular class.

#include <iostream.h>

  class Student
      {
          protected:
             char sname[20];
             int rn;
          public:
             void getData()
                 {
                      cout<<"\n\tRoll No.? ";
                      cin>>rn;
                      cin.ignore();
                      cout<<"\n\tStudent Name? ";
                      cin.get(sname,20);
                 }
             void Disp()
                 {
                      cout<<"\n\n\t\t\t* * Marksheet * *\n";
                      cout<<"\n\n\t\tRoll : "<<rn<<"\tName : "<<sname;
                 }
      };
  class Marks: public Student
      {
          protected:
             int m[5],tot;
          public:
             Marks()
                 {
                      tot=0;
                 }
             void getMarks()
                 {
                      char sub[6][10]={"Eng","Maths","Phy","Chem" ,"Comp","Lang"};
                      for(int i=0;i<5;i++)
                         {
                              cout<<"\n\t\tMarks for "<<sub[i]<<" : ";
                              cin>>m[i];
                              tot=tot+m[i];
                         }
                 }
      };
  class Extra_curricular
      {
          protected:
             int score;
          public:
             void getScore()
                 {
                      cout<<"\n\tEnter Sports Score ( Between 1 - 10 ) : ";
                      cin>>score;
                 }
      };

  class Result:public Extra_curricular,public Marks
      {
          private:
             char grade;
             float avg;
          public:

             void calResult()
                 {
                      //getData();
                      if(score>=8)
                         {
                              tot=tot+60;
                         }
                      else if(score>=5)
                         {
                              tot=tot+45;
                         }
                      else if(score>=3)
                         {
                              tot=tot+30;
                         }
                      else
                         {
                              tot=tot+10;
                         }

                      avg=tot/5.0;
                      if(avg<35)
                         {
                              grade='F';
                         }
                      else if(avg<46)
                         {
                              grade='E';
                         }
                      else if(avg<60)
                         {
                              grade='D';
                         }
                      else if(avg<80)
                         {
                              grade='C';
                         }
                      else if(avg<90)
                         {
                              grade='B';
                         }
                      else
                         {
                              grade='A';
                         }

                 }
             void DispResult()
                 {
                      Disp();
                      char sub[6][10]={"Eng","Maths","Phy","Chem" ,"Comp","Lang"};
                      cout<<"\n\t\tSubject  Marks";
                      for(int i=0;i<5;i++)
                         {
                              cout<<"\n\t\t"<<sub[i]<<"\t  "<<m[i];
                         }
                      cout<<"\n\t\tSport Scoring : "<<score;
                      cout<<"\n\n\t\tTotal :"<<tot<<"  Average : "<<avg;
                      cout<<"\n\n\t\tGrade : "<<grade;
                      if(grade=='F')
                         {
                              cout<<"\t\t* * Not Promoted ";
                         }
                      else
                         {
                              cout<<"\t\t* * Promoted";
                         }
                 }
      };

  int main()
      {
          Result R;
          R.getData();
          R.getMarks();
          R.getScore();
          R.calResult();
          R.DispResult();
          return 0;
      }

No comments: