You are on page 1of 5

a1

1 Write a C++ program to create a Base class called STUDENT(name, rollno, age)
and
using inheritance create classes UG Student and PG Student having fields as semester,
fees, stipend. Enter data of 6 students. Find the average age semester wise for all UG
and
PG Students separately.(Assume at least two different values for semester field for
each
UG and PG Classes).

#include<iostream>
#include<conio.h>
using namespace std;
class Student
{
protected:
char name [20];
int rn,age;
};

class Ug : public Student


{
int sem,fee,sti;

public:
void getdata()
{
cout<<"\nEnter Roll Number : " ;
cin>>rn;
cout<<"\nEnter Name: ";
cin>>name;
cout<<"\nEnter Age : ";
cin>>age;
cout<<"\nEnter Semester : ";
cin>>sem;
cout<<"\nEnter Fee : ";
cin>>fee;
cout<<"\nEnter Stipend : ";
cin>>sti;
}

void display()
Page 1
a1
{
cout<<name<<"\t"<<age<<"\t"<<sem<<"\n";
}

int getsem()
{
return(sem);
}

int getage()
{
return(age);
}

};

class Pg : public Student


{
int sem,fee,sti;

public:
void getdata()
{
cout<<"\nEnter Roll Number : " ;
cin>>rn;
cout<<"\nEnter Name: ";
cin>>name;
cout<<"\nEnter Age : ";
cin>>age;
cout<<"\nEnter Semester : ";
cin>>sem;
cout<<"\nEnter Fee : ";
cin>>fee;
cout<<"\nEnter Stipend : ";
cin>>sti;
}

void display()
{
cout<<name<<"\t"<<age<<"\t"<<sem<<"\n";
}

Page 2
a1
int getsem()
{
return(sem);
}

int getage()
{
return(age);
}

};

int main()
{

Pg pg[6];
Ug ug[6];

int flag,age,sem,agesum=0,semcnt=0,i,j;

cout<<"\n\nEnter Post Graduate students details..\n";


for(i=0; i<6; i++ )
pg[i].getdata();

cout<<"\n\nEnter Under Graduate students details..\n";


for(i=0; i<6; i++ )
ug[i].getdata();

cout<<"\nUG - students details..\n";


cout<<"--------------------------\n";
cout<<"Name\tAge\tSem\n";
for(i=0; i<6; i++ )
ug[i].display();
cout<<"--------------------------\n";
cout<<"The sem wise average age (UG):"<<endl;
cout<<"sem age"<<endl;
for(i=1; i<9; i++ )
{
flag=0;
for(j=0; j<6; j++ )
{
Page 3
a1
sem=ug[j].getsem();
age=ug[j].getage();

if( i==sem )
{
agesum+=age;
semcnt++;
flag=1;
}
}
if(flag)
cout<<i<<"\t"<<agesum/semcnt<<endl;
}

agesum=0,semcnt=0;

cout<<"\n\nPG - students details..\n";


cout<<"--------------------------\n";
cout<<"Name\tAge\tSem\n";
for(i=0; i<6; i++ )
pg[i].display();
cout<<"--------------------------\n";
cout<<"The sem wise average age (PG):"<<endl;
cout<<"sem age"<<endl;
for(i=1; i<9; i++ )
{
flag=0;
for(j=0; j<6; j++ )
{
sem=pg[j].getsem();
age=pg[j].getage();

if( i==sem )
{
agesum+=age;
semcnt++;
flag=1;
}
}
if(flag)
cout<<i<<"\t"<<agesum/semcnt<<endl;
}
Page 4
a1

getch();
return 0;

Page 5

You might also like