Home | | Computer Science 11th std | C++ Hybrid inheritance Program

Chapter: 11th Computer Science : Chapter 16 : Inheritance

C++ Hybrid inheritance Program

This inheritance is a combination of multi level and multiple inheritance and so it is called hybrid inheritance



Illustration 16.5 HYBRID inheritance

# include <iostream>

using namespace std;

class student //base class

{

private :

char name[20];

int rno;

public:

void acceptname()

{

cout<<"\n Enter roll no and name .. ";

cin>>rno>>name;

}

void displayname()

{

cout<<"\n Roll no :-"<<rno;

cout<<"\n Name :-"<<name<<endl;

}

};

class exam : public student //derived class with single base class

{

public:

int mark1, mark2 ,mark3,mark4,mark5,mark6;

void acceptmark()

{

cout<<"\n Enter lang,eng,phy,che,csc,mat marks.. ";

cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;

}

void displaymark() {

cout<<"\n\t\t Marks Obtained ";

cout<<"\n Language.. "<<mark1;

cout<<"\n English .. "<<mark2;

cout<<"\n Physics .. "<<mark3;

cout<<"\n Chemistry.. "<<mark4;

cout<<"\n Comp.sci.. "<<mark5;

cout<<"\n Maths ..  "<<mark6;

}

};

class detail                   //base classs 2

{

int dd,mm,yy;

char cl[4];

public:

void acceptdob()

{

cout<<"\n Enter date,month,year in digits and class .. ";

cin>>dd>>mm>>yy>>cl;

}

void displaydob()

{

cout<<"\n class         :-"<<cl;

cout<<"\t\t DOB        : "<<dd<<" - "<<mm<<" -" <<yy<<endl;

}

};

class result : public exam,public detail

//inherits from exam ,which itself is a

//derived class and also from class detail

{

      int total;

      public:

      void showresult()

      {

      total=mark1+mark2+mark3+mark4+mark5+mark6;

      cout<<"\nTOTAL MARK SCORED : "<<total;

      }

};

class detail  //base classs 2

{

int dd,mm,yy;

char cl[4];

public:

void acceptdob()

{

cout<<"\n Enter date,month,year in digits and class .. ";

cin>>dd>>mm>>yy>>cl;

}

void displaydob()

{

cout<<"\n class        :-"<<cl;

cout<<"\t\t DOB      : "<<dd<<" - "<<mm<<" -" <<yy<<endl;

}

};

class result : public exam,public detail

//inherits from exam ,which itself is a

//derived class and also from class detail

{

      int total;

      public:

           void showresult()

      {

           total=mark1+mark2+mark3+mark4+mark5+mark6;

           cout<<"\nTOTAL MARK SCORED : "<<total;

      }

};

int main()

{

result r1;

r1.acceptname();         //calling base class function using derived class object

r1.acceptmark();         //calling base class which itsel is a derived class function

using its derived class object

int main()

{

result r1;

r1.acceptname();             //calling base class function using derived class object

r1.acceptmark();             //calling base class which itsel is a derived class function

using its derived class object

r1.acceptdob();

cout<<"\n\n\t\t MARKS STATEMENT";

r1.displayname();             //calling base class function using derived class object

r1.displaydob();

r1.displaymark();             //calling base class which itsel is a derived class function using its derived class object

r1.showresult();              //calling the child class function

return 0;

}

Output:

Enter roll no and name .. 1201 RAGU

Enter lang,eng,phy,che,csc,mat marks.. 96 98 100 100 100 100

Enter date,month,year in digits and class .. 7 12 2001 XII

MARKS STATEMENT

Roll no :-1201

Name :-RAGU

class   :-XII    DOB          : 7 - 12 -2001

Marks Obtained

Language.. 96

English .. 98

Physics .. 100

Chemistry.. 100

Comp.sci.. 100

Maths ..  100

TOTAL MARK SCORED      : 594

 

In the above program the derived class “result” has acquired the properties of class “detail” and class “exam” which is derived from “student”. So this inheritance is a combination of multi level and multiple inheritance and so it is called hybrid inheritance

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 16 : Inheritance : C++ Hybrid inheritance Program |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.