Write a program to create a class called STUDENT with data members Roll
Number, Name and Age. Using inheritance, create the classes
UGSTUDENT and PGSTUDENT having fields a semester, fees and
stipend. Enter the data for at least 5 students. Find the average age for all
UG and PG students separately.
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
public: int R,A;
char N[20];
void take_data();
};
class ugstudent:public student
{
public: int stip,semester;
float fee;
void take_data();
};
class pgstudent:public student
{
public: int stip,semester;
float fee;
void take_data();
};
void student::take_data()
{
cout<<"\nInput name:";
cin>>N;
cout<<"\nInput Reg.no.";
cin>>R;
cout<<"\nInput age:";
cin>>A;
}
void ugstudent::take_data()
{
student::take_data();
cout<<"\nInput the semester:";
cin>>semester;
cout<<"\nInput the fees:";
cin>>fee;
cout<<"\nInput the stipend:";
cin>>stip;
}
void pgstudent::take_data()
{
student::take_data();
cout<<"\nInput the semester:";
cin>>semester;
cout<<"\nInput the fees:";
cin>>fee;
cout<<"\nInput the stipend:";
cin>>stip;
}
int main()
{
ugstudent ug[20];
pgstudent pg[20];
int x,y,m;
float average;
cout<<"\nInput the number of entries in the ugstudent class:";
cin>>y;
for(x=1;x<=y;x++)
ug[x].take_data();
for(int semester=1;semester<=8;semester++)
{
float add=0;
int get=0,count=0;
for(x=1;x<=y;x++)
{
if(ug[x].semester==semester)
{
add=add+ug[x].A;
get=1;
count++; }
}
if(get==1)
{
average=add/count;
cout<<"\nAverage age of semester "<<semester<<" is: "<<average; }
}
cout<<"\ninput the number of entries of pgstudent class:";
cin>>y;
for(x=1;x<=y;x++)
pg[x].take_data();
for(int semester=1;semester<=8;semester++)
{
float add=0;
int get=0,count=0;
for(x=1;x<=y;x++)
{
if(pg[x].semester==semester)
{
add=add+pg[x].A;
get=1;
count++; }
}
if(get==1)
{
average=add/count;
cout<<"\Average age of Semester "<<semester<<" is "<<average; }
}
getch();
}
Comments
Leave a comment