Object Oriented Programming Practical 13
Question 1
Write a C++ program to implement given class hierarchy.
player.cpp
C
#include<iostream.h>
#include<conio.h>
class player
{
public:
char name[50];
int matches;
void accept()
{
cout<<"Enter name of player: ";
cin>>name;
cout<<"Enter number of matches: ";
cin>>matches;
}
void display()
{
cout<<"Name of player: "<<name<<endl;
cout<<"Number of matches: "<<matches<<endl;
}
};
class batsman:public player
{
int total_score,match_score;
float avg;
public:
void calculate_average()
{
cout<<"Enter total score: ";
cin>>total_score;
cout<<"Enter match score: ";
cin>>match_score;
avg=(float)total_score/matches;
}
void display()
{
player::display();
cout<<"Batsman total score: "<<total_score<<endl;
cout<<"Batsman match score: "<<match_score<<endl;
cout<<"Batsman average score: "<<avg<<endl;
}
};
class bowler:public player
{
int wickets;
public:
void getbowler()
{
cout<<"Enter number of wickets: ";
cin>>wickets;
}
void display()
{
player::display();
cout<<"Bowler number of wickets: "<<wickets;
}
};
int main()
{
clrscr();
batsman bt;
cout<<"\nEnter batsman details"<<endl;
bt.accept();
bt.calculate_average();
bowler bl;
cout<<"\nEnter bowler details"<<endl;
bl.accept();
bl.getbowler();
cout<<"\n-------------------Batsman details-----------------"<<endl;
bt.display();
cout<<"\n-------------------Bowler details:-----------------"<<endl;
bl.display();
getch();
return 0;
}
Output

Question 2
Write a C++ program to implement given figure class hierarchy.
staff.cpp
C
#include<iostream.h>
#include<conio.h>
class staff
{
int code;
public:
void accept()
{
cout<<"\nEnter code of staff: ";
cin>>code;
}
void display()
{
cout<<"\nCode of staff: "<<code;
}
};
class teacher:public staff
{
char subject[50];
public:
void getteacher()
{
cout<<"\nEnter subject: ";
cin>>subject;
}
void putteacher()
{
cout<<"\nTeacher subject: "<<subject;
}
};
class officer:public staff
{
char grade[50];
public:
void getofficer()
{
cout<<"\nEnter officer grade: ";
cin>>grade;
}
void putofficer()
{
cout<<"\nOfficer grade: "<<grade;
}
};
int main()
{
clrscr();
teacher t;
cout<<"Enter teacher details"<<endl;
t.getteacher();
officer o;
cout<<"Enter officer details"<<endl;
o.getofficer();
cout<<"\n--------------Teacher details---------------"<<endl;
t.putteacher();
cout<<"\n--------------Officer details---------------"<<endl;
o.putofficer();
getch();
return 0;
}
Output
