A 1
A 1
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;
};
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);
}
};
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;
if( i==sem )
{
agesum+=age;
semcnt++;
flag=1;
}
}
if(flag)
cout<<i<<"\t"<<agesum/semcnt<<endl;
}
agesum=0,semcnt=0;
if( i==sem )
{
agesum+=age;
semcnt++;
flag=1;
}
}
if(flag)
cout<<i<<"\t"<<agesum/semcnt<<endl;
}
Page 4
a1
getch();
return 0;
Page 5