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

Chapter: 11th Computer Science : Chapter 16 : Inheritance

C++ Hierarchical inheritance Program

Here for single base class more than one derived class.So this comes under hierarchical inheritance.



Illustration 16.4 Hierarchical 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 qexam : 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 for quarterly exam.. ";

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

}

void displaymark()

{

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

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 hexam : 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 for halfyearly exam.. ";

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

}

void displaymark()

{

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

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;

}

};

int main()

{

qexam q1;

hexam h1;

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

q1.acceptmark();        //calling base class function

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

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

h1.acceptmark();

h1.displaymark();         //calling base class function using its

//derived class object

return 0;

}

Output

Enter roll no and name .. 1201 KANNAN

Enter lang,eng,phy,che,csc,mat marks for quarterly exam..

95 96 100 98 100 99

Roll no :-1201

Name :-KANNAN

Marks Obtained in quarterly

Language.. 95

English .. 96

Physics .. 100

Chemistry.. 98

Comp.sci.. 100

Maths ..  99

Enter roll no and name .. 1201 KANNAN

Enter lang,eng,phy,che,csc,mat marks for halfyearly exam..

96 98 100 100 100 100

Roll no :-1201

Name :-KANNAN

Marks Obtained in Halfyearly

Language.. 96

English .. 98

Physics .. 100

Chemistry.. 100

Comp.sci.. 100

Maths ..  100

 

In the above program the class “qexam” and “hexam” are derived from class “student”. Here for single base class more than one derived class.So this comes under hierarchical inheritance.

 

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


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

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