C++ Program to implement Hierarchical Inheritance (Part 2) – Q21
Q21. Program to implement Hierarchical Inheritance (Part 2):
A university is associated with three different kinds of students Undergraduate , Post-Graduate and Doctoral.
– Undergraduate students have grades awarded to the for individual subjects.
– Postgraduate students sit for 8 papers and deliver a seminar. They are awarded grades for these.
– Doctoral students sit for 4 papers and write a thesis. They are awarded grades for the papers but for the thesis the evaluation is only whether the thesis is accepted or not.
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
enum boolean {false, true};
class Cstud{
protected:
char name[20];
};
class Cunderg : public Cstud{
private:
int papers[5];
char grade;
public:
int tot, avg;
void getdata(){
cout<<"\n Enter Name: ";
cin>>name;
cout<<"\n Enter Marks in 5 Subjects: -"<<endl;
tot = 0;
for(int i=0; i<5; i++){
cin>>papers[i];
tot += papers[i];
}
avg = tot / 5;
if(avg >= 80 && avg < 100) grade = 'A';
else if(avg > 60 && avg < 80) grade = 'B';
else if(avg > 40 && avg < 60) grade = 'C';
else grade = 'D';
}
void showdata(){
cout<<"\n Name: "<<name;
cout<<"\n Total Marks: "<<tot;
cout<<"\n Grade: "<<grade;
}
};
class Cpostg : public Cstud{
private:
int papers[8], sem;
char grade;
public:
int tot;
void getdata(){
cout<<"\n Enter Name: ";
cin>>name;
cout<<"\n Enter Marks in 8 Subjects: -"<<endl;
tot = 0;
for(int i=0; i<8; i++){
cin>>papers[i];
tot += papers[i];
}
cout<<"\n Enter Marks of Seminar: ";
cin>>sem;
if(sem >= 80 && sem < 100) grade = 'A';
else if(sem > 60 && sem < 80) grade = 'B';
else if(sem > 40 && sem < 60) grade = 'C';
else grade = 'D';
}
void showdata(){
cout<<"\n Name: "<<name;
cout<<"\n Total Marks in 8 Papers: "<<tot;
cout<<"\n Seminar Score: "<<sem;
cout<<"\n Grade: "<<grade;
}
};
class Cdoctor : virtual public Cstud{
private:
int papers[4];
boolean thesis;
char grade;
public:
int tot, avg;
char ch;
void getdata(){
cout<<"\n Enter Name: ";
cin>>name;
cout<<"\n Enter Marks in 4 Subjects: -"<<endl;
tot = 0;
for(int i=0; i<4; i++){
cin>>papers[i];
tot += papers[i];
}
avg = tot / 4;
if(avg >= 80 && avg < 100) grade = 'A';
else if(avg > 60 && avg < 80) grade = 'B';
else if(avg > 40 && avg < 60) grade = 'C';
else grade = 'D';
cout<<"\n Is the Thesis Accepted (y/n): ";
cin>>ch;
if(ch == 'y') thesis = true;
else thesis = false;
}
void showdata(){
cout<<"\n Name: "<<name;
cout<<"\n Total Marks: "<<tot;
cout<<"\n Grade: "<<grade;
cout<<"\n Thesis: ";
if(thesis) cout<<"Accepted";
else cout<<"Not Accepted";
}
};
void main(){
int ch;
while(1){
clrscr();
cout<<"\n UNIVERSITY";
cout<<"\n ~~~~~~~~~~";
cout<<"\n 1 -> Under Graduates.";
cout<<"\n 2 -> Post Graduates.";
cout<<"\n 3 -> Doctoral.";
cout<<"\n 4 -> Exit.";
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch){
case 1: // Under-Graduates.
Cunderg Oug;
Oug.getdata();
Oug.showdata();
break;
case 2: // Post-Graduates.
Cpostg Opg;
Opg.getdata();
Opg.showdata();
break;
case 3: // Doctoral.
Cdoctor Od;
Od.getdata();
Od.showdata();
break;
default:
exit(1);
} // end of switch.
getch();
} // end of while.
} // end of main.
Output:
UNIVERSITY
1 -> Under Graduates.
2 -> Post Graduates.
3 -> Doctoral.
4 -> Exit.
Enter your choice: 1
Enter Name: Nitin
Enter Marks in 5 Subjects: –
200
300
400
350
150
Name: Nitin
Total Marks: 1400
Grade: D
UNIVERSITY
1 -> Under Graduates.
2 -> Post Graduates.
3 -> Doctoral.
4 -> Exit.
Enter your choice: 2
Enter Name: Manoj
Enter Marks in 8 Subjects: –
200
300
250
350
150
400
200
100
Enter Marks of Seminar: 400
Name: Manoj
Total Marks in 8 Papers: 1950
Seminar Score: 400
Grade: D




