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 <string>
using namespace std;
class Student{
int roll_no, age;
string name;
public:
Student(){}
Student(int roll, int age, string name){
roll_no = roll;
this->age = age;
this->name = name;
}
int getAge(){
return this->age;
}
};
class UgStudent: public Student{
int semester, fees, stipend;
public:
UgStudent(int roll, int age, string name, int semester, int fees, int stipend):Student(roll, age, name){
this->semester = semester;
this->fees = fees;
this->stipend = stipend;
}
};
class PgStudent: public Student{
int semester, fees, stipend;
public:
PgStudent(int roll, int age, string name, int semester, int fees, int stipend):Student(roll, age, name){
this->semester = semester;
this->fees = fees;
this->stipend = stipend;
}
};
int main(){
PgStudent one(1, 26, "Lucious Fox", 2, 2000, 32),
two(2, 28, "Robin Johnson", 3, 3222, 7);
UgStudent three(3, 24, "Robin Johnson", 2, 3222, 2),
four(2, 22, "Robin Johnson", 1, 3222, 5),
five(2, 22, "Robin Johnson", 1, 3222, 34);
cout<<"Average age = "<<(one.getAge()+two.getAge()+three.getAge()+four.getAge()+five.getAge()) / 5<<endl;
return 0;
}
Comments
Leave a comment