Hybrid Inheritance


There could be cases where we need to apply more than two types of inheritances to design a program. when we apply two or more types of i inheritances n a single program then it is called a hybrid inheritance.

For Example :We wanted to write a simple program that could perform processing of students results.

Block Diagram for Hybrid Inheritance












Program:
#include<iostream>

using namespace std;

class student
{
 protected:
   int rollno;
   char name[30];
 public:
   void getdata()
   {
    cout<<"\nEnter student's Name:";
    cin>>name;
    cout<<"enter the rollno:";
    cin>>rollno;
   }
  void display_details()
  {
   cout<<"\nName:"<<name;
   cout<<“\nRoll no:"<<rollno;
  }
};

class test: public student
{
 protected:
  float sub1,sub2;
 public:
 void get_marks()
 {
  cout<<endl<<"student subject 1=";
  cin>>sub1;
  cout<<"subject 2=";
  cin>>sub2;
 }
 void disp_marks()
 {
  cout<<“\nSubject 1="<<sub1;
  cout<<"\nSubject 2="<<sub2;
 }
};

class sports
{
 protected:
  float score;
 public:
  void get_score()
  {
   cout<<"\nEnter the score:";
   cin>>score;
  }
  void disp_score()
  {
   cout<<endl<<"score="<<score;
  }
};

class result:public test,public sports
{
  float total;
 public:
  void display()
  {
   display_details();
   disp_marks();
   disp_score();
   total=sub1+sub2+score;
  cout<<"\nTOTAL score="<<total;
  }
};

int main()
{
 int n;
 cout<<"Enter the number of students:";
 cin>>n;
 result s[70];
 for(int i=0;i<n;i++)
 {
  cout<<"\nSTUDENT NO:"<<i+1;
  s[i].getdata();
  s[i].get_marks();
  s[i].get_score();
}
 cout<<endl<<endl;
for(int i=0;i<5;i++)
 {
  cout<<"\nSTUDENT NO:"<<i+1;
  s[i].display();
}
  return 0;

Comments

Popular posts from this blog

Refrigeration By ice cutting.

How to check if you're buying a stolen phone.

Life after Chernobyl