Make Student an abstract class. Declare gpa() and display() as pure virtual functions in it. Derive semester student, courses student, and new student from base class Student. The display () function should show employee no, employee name, and salary of all employees. (Friend Functions). All the information is store in student.txt and handle through file handling.
#include<iostream>
#include<string>
#include<fstream>
//#include<vector>
/*File student.txt
John 123 3000 67 S
Mike 232 2500 89 C
Jack 111 3000 90 S
Mary 332 1500 59 N
Ioan 199 2700 88 S
Nick 201 2800 89 C
Natan 101 1800 73 S
Julia 340 1400 65 N
*/
using namespace std;
class Student
{
public:
virtual double gpa()=0;
virtual void Display()=0;
};
class SemestrStudent:public Student
{
string emplName;
int emplNo;
double salary;
double Sgpa;
public:
SemestrStudent(string _emplName, int _emplNo, double _salary, double _Sgpa)
:emplName(_emplName),emplNo(_emplNo),salary(_salary),Sgpa(_Sgpa){}
double gpa(){return Sgpa;}
void Display()
{
cout<<"\nEmployee name "<<emplName
<<"\nEmployee nomber "<<emplNo
<<"\nEmployee salary "<<salary;
}
};
class CoursesStudent:public Student
{
string emplName;
int emplNo;
double salary;
double Sgpa;
public:
CoursesStudent(string _emplName, int _emplNo, double _salary, double _Sgpa)
:emplName(_emplName),emplNo(_emplNo),salary(_salary),Sgpa(_Sgpa){}
double gpa(){return Sgpa;}
void Display()
{
cout<<"\nEmployee name "<<emplName
<<"\nEmployee nomber "<<emplNo
<<"\nEmployee salary "<<salary;
}
};
class NewStudent:public Student
{
string emplName;
int emplNo;
double salary;
double Sgpa;
public:
NewStudent(string _emplName, int _emplNo, double _salary, double _Sgpa)
:emplName(_emplName),emplNo(_emplNo),salary(_salary),Sgpa(_Sgpa){}
double gpa(){return Sgpa;}
void Display()
{
cout<<"\nEmployee name "<<emplName
<<"\nEmployee nomber "<<emplNo
<<"\nEmployee salary "<<salary;
}
};
int main()
{
ifstream ifs("student.txt");
if(!ifs.is_open())
cout<<"File can not be opened";
else
{
string line;
while(getline(ifs,line,'\n'))
{
string eName=line.substr(0,line.find(' '));
int pos =line.find(' ');
line=line.substr(pos+1);
string eNo=line.substr(0,line.find(' '));
pos =line.find(' ');
line=line.substr(pos+1);
string esal=line.substr(0,line.find(' '));
pos =line.find(' ');
line=line.substr(pos+1);
string egpa=line.substr(0,line.find(' '));
pos =line.find(' ');
line=line.substr(pos+1);
char type=line[0];
if(type=='S')
{
SemestrStudent tmp(eName,stoi(eNo),stod(esal),stod(egpa));
Student* p=&tmp;
p->Display();
cout<<"\nGpa="<<p->gpa();
}
else if(type=='C')
{
CoursesStudent tmp(eName,stoi(eNo),stod(esal),stod(egpa));
Student* p=&tmp;
p->Display();
cout<<"\nGpa="<<p->gpa();
}
else if(type=='N')
{
NewStudent tmp(eName,stoi(eNo),stod(esal),stod(egpa));
Student* p=&tmp;
p->Display();
cout<<"\nGpa="<<p->gpa();
}
}
}
}
Comments
Leave a comment