Write a C++ program to create a base class called STUDENT (Name, Roll Number, Age) and using inheritance create classes UG student and PG student having fields as semester, fees and stipend. Enter the data of 5 students. Store each set of data to file. Find the average age, semester wise for all UG and PG students separately from file. (Assume at least two different values for the semester field for each of UG and PG classes).
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Student {
private:
string name;
string rollNumber;
int age;
public:
Student() {
}
Student(string name, string rollNumber, int age) {
this->name = name;
this->rollNumber = rollNumber;
this->age = age;
}
void getdata()
{
cout<<"Enter Name: ";
cin>>name;
cout<<"Enter Roll Number : " ;
cin>>rollNumber;
cout<<"Enter Age : ";
cin>>age;
}
virtual void display() {
cout<<"Name: " <<this->name <<"\n";
cout<<"Roll number: " << this->rollNumber << "\n";
cout<<"Age: " << this->age<< "\n";
}
string getName(){
return name;
}
string getRollNumber(){
return rollNumber;
}
int getAge(){
return age;
}
};
class UG :public Student {
private:
int semester;
int fees;
int stipend;
public:
UG(){}
UG (string name, string rollNumber, int age,int semester,int fees,int stipend):Student(name,rollNumber,age) {
this->semester = semester;
this->fees = fees;
this->stipend = stipend;
}
void getdata()
{
Student::getdata();
cout<<"Enter semester: ";
cin>>semester;
cout<<"Enter fees: " ;
cin>>fees;
cout<<"Enter stipend: ";
cin>>stipend;
}
void display() {
cout<<"UG Student\n";
Student::display();
cout<<"Semester: " <<this->semester <<"\n";
cout<<"Fees: " << this->fees << "\n";
cout<<"Stipend: " << this->stipend<< "\n";
}
int getSemester(){
return this->semester;
}
int getFees(){
return this->fees;
};
int getStipend(){
return this->stipend;
}
};
class PG :public Student {
private:
string semester;
int fees;
int stipend;
public:
PG(){}
PG(string name, string rollNumber, int age,string semester,int fees,int stipend):Student(name,rollNumber,age) {
this->semester = semester;
this->fees = fees;
this->stipend = stipend;
}
void getdata()
{
Student::getdata();
cout<<"Enter semester: ";
getline(cin,semester);
cin.ignore();
cout<<"Enter fees: " ;
cin>>fees;
cout<<"Enter stipend: ";
cin>>stipend;
}
void display() {
cout<<"PG Student\n";
Student::display();
cout<<"Semester: " <<this->semester <<"\n";
cout<<"Fees: " << this->fees << "\n";
cout<<"Stipend: " << this->stipend<< "\n";
}
string getSemester(){
return this->semester;
}
int getFees(){
return this->fees;
};
int getStipend(){
return this->stipend;
}
};
int main() {
float sumUG=0;
float sumPG=0;
Student** students=new Student*[5];
ofstream studentsFile;
studentsFile.open ("Students.txt");
for(int i=0;i<2;i++){
UG* _UG=new UG();
_UG->getdata();
students[i]=_UG;
studentsFile <<_UG->getName()<<","<<_UG->getRollNumber()<<","<<_UG->getAge()<<","
<<_UG->getSemester()<<","<<_UG->getFees()<<","<<_UG->getStipend()<< "\n";
sumUG+=_UG->getAge();
}
for(int i=2;i<5;i++){
PG* _PG=new PG();
_PG->getdata();
students[i]=_PG;
studentsFile <<_PG->getName()<<","<<_PG->getRollNumber()<<","<<_PG->getAge()<<","
<<_PG->getSemester()<<","<<_PG->getFees()<<","<<_PG->getStipend()<< "\n";
sumPG+=_PG->getAge();
}
studentsFile.close();
float averageUG=sumUG/2;
float averagePG=sumPG/3;
cout<<"The average age for UG: "<<averageUG<<"\n";
cout<<"The average age for PG: "<<averagePG<<"\n";
cout<<"All students\n";
for(int i=0;i<5;i++){
students[i]->display();
cout<<"\n";
}
cin>>averageUG;
return 0;
}
Comments
Leave a comment