This program enforces the concept of inheritance and polymorphism which are parts of the Object Oriented Design of the C++ language. The Program accesses a file named roster.dat which must be placed in the same directory in which this source file is executed. The File roster.dat is attached as a separate file, without this file in the same directory as the source file, the program will throw a file not found error and the file handling aspect of the program will not be executed. So before running this program, simple open notepad and click as as roster.dat in the same directory for the program.

#include <iostream>
#include <iomanip>
#include <string>
#include <list>
#include <algorithm>
#include <fstream>
#include <vector>
using namespace std;

//BASE CLASS STUDENT DEFINES THE GENERAL DATA MEMBERS AND FUNCTIONS OF THE CLASS
class Student
{
public: //all member functions here are accessible to DCL.
virtual float tuition() const=0; //PURE VIRTUAL FUNCION TO BE OVERRIDEN
Student(const string &name,const string &ssn,const string &year, int
&credits ,float &gpa)//CONSTRUCTOR
{
this->name = name;
this->ssn = ssn;
this->year = year;
this->credits = credits;
this->gpa= gpa;
}
string getName(){return name;}
string getSSN(){return ssn;}
string getYear(){return year;}
int getCredit(){return credits;}
float getGpa(){return gpa;}
virtual void print()const //VIRTUAL FUNCTION ,general printing work
{
cout<<“Student Name :”<<name<<endl;
cout<<“Social Security:”<<ssn<<endl;
cout<<“Year :”<<year<<endl;
cout<<“Credit :”<<credits<<endl;
cout<<“GPA :”<<gpa<<endl;
}
protected://Data declared here is accessible to the base and DCL only.
string name;//stores the name of a student
string ssn;//stores the social security number of a student
string year;//stores the students year .
float gpa;//stores student’s gpa
int credits;//stores student’s credit

};

//CLASS UNDERGRAD DERIVED FROM THE BASE CLASS STUDENTS
class Undergrad : public Student
{
public:
Undergrad(const string &name, const string &ssn,const string year,
int credits,float gpa):
Student(name,ssn,year,credits,gpa){}
void setRate(float under_rate){
this->undergrad_rate = under_rate;
}
float tuition()const
{
return undergrad_rate * credits;
}
void print()
{
Student::print();
cout<<“Tuition :”<<tuition()<<endl<<endl<<endl;
}
private:
float undergrad_rate,under_rate;
};
class Grad:public Student
{
public:
Grad(string name,string ssn,string year,int credits, float gpa,
string thesis):
Student(name,ssn,year,credits,gpa){this->thesis = thesis;}
void setRate(float g_rate){
this->grad_rate = g_rate;
}
float tuition()const
{
return grad_rate * credits;
}
void print()const
{
Student::print();
cout<<“Tuition :”<<tuition()<<endl;
cout<<“Thesis :”<<thesis<<endl;
}
private:
float grad_rate;
string thesis;
};
class GradAsst:public Grad
{
public:
GradAsst(string name,string ssn,string year,int credits, float gpa,
string thesis,string task,float hourPay, string supervisor):
Grad(name,ssn,year,credits,gpa,thesis)
{
this->task = task;
this->hourPay = hourPay;
this->supervisor = supervisor;
}
void print()const
{
cout<<endl<<endl;
Grad::print();
cout<<setw(10)<<“Task :”<<task<<endl;
cout<<setw(10)<<“Hourly Pay :$”<<hourPay<<endl;
cout<<setw(10)<<“Supervisor :”<<supervisor<<endl<<endl<<endl;
}
private:
float hourPay;
string task,supervisor;
};
bool sortByGpa(Undergrad a, Undergrad b)
{
return a.getGpa() < b.getGpa();
}
int main()

{
Student *nxt;//Base class pointer for dynamic operations;
float const under_rate = 380.0;
float const g_rate = 500;
Undergrad s1(“Mary”,”000111222″,”Junior”,12,4.0 );
Grad s2(“David”,”111222333″,”Graduate”,9,3.7,
“How to learn data structures using C++/STL?”);
GradAsst s3(“Jason”,”222333444″,”Graduate Assistance”,9,3.9,
“Design of efficient algorithms”,
“Grading 330 projects and implementing a data mining algorithm”,20,
“Dr. Fu”);
s1.setRate(under_rate);
s2.setRate(g_rate);
s3.setRate(g_rate);
//Static Calling of the Print function
cout<<“Static calling of the Print function”<<endl<<endl;
s1.print();
s2.print();
s3.print();
//dynamic calling of the print function
cout<<endl<<endl<<“Dynamic Calling of the Print function”<<endl<<endl;
nxt = &s1;
nxt->print();//Dynamic call to print
cout<<endl<<endl;
nxt = &s2;
nxt->print();
cout<<endl<<endl;
nxt = &s3;
nxt->print();
//Static calling of the tution function
cout<<endl<<endl<<“Static Calling of the Tuition function”<<endl<<endl;
cout<<s1.getName()<<“‘s Tuition:”<<s1.tuition()<<endl;
cout<<s2.getName()<<“‘s Tuition:”<<s2.tuition()<<endl;
cout<<s3.getName()<<“‘s Tuition:”<<s3.tuition()<<endl;
//Dynamic calling of the tuition function
cout<<endl<<endl<<“Dynamic Calling of the Tuition function”<<endl<<endl;
nxt = &s1;
cout<<s1.getName()<<“‘s Tuition:”<<nxt->tuition()<<endl;
nxt = &s2;
cout<<s2.getName()<<“‘s Tuition:”<<nxt->tuition()<<endl;
nxt = &s3;
cout<<s3.getName()<<“‘s Tuition:”<<nxt->tuition()<<endl;
//File handling
ifstream fin;
fin.open(“roster.dat”, ios::in);
if(!fin){
cerr<<“File could not be opened”<<endl;
exit(1);
}
string name,ssn,year;
int credits;
float gpa;
list<Undergrad> undergrad;
while(fin>>name>>ssn>>year>>credits>>gpa)
{
Undergrad s(name,ssn,year,credits,gpa);
undergrad.push_back(s);
}
list<Undergrad>::const_iterator iter;

cout<<endl<<endl<<setw(50)<<“Original Students Roster”<<endl<<endl;
cout<<setiosflags(ios::left)<<setw(20)<<“Name”<<setw(10)
<<“SSN”<<setw(10)<<“Year”<<setw(10)<<“Credits”<<resetiosflags(ios::left)
<<setiosflags(ios::right)<<setw(10)<<“Tuition”<<setw(10)<<“GPA”<<endl<<endl;

for( iter = undergrad.begin();iter != undergrad.end();iter++)
{
Undergrad s = *iter;
s.setRate(under_rate);
cout<<resetiosflags(ios::right)<<setiosflags(ios::left)<<setw(20)
<<s.getName()<<setw(10)<<s.getSSN()<<setw(10)<<s.getYear()<<setw(10)
<<s.getCredit()<<resetiosflags(ios::left)<<setiosflags(ios::right)
<<setiosflags(ios::fixed)<<setprecision(2)<<setw(10)<<setfill(‘$’)
<<s.tuition()<<setfill(‘ ‘)<<setw(10)<<s.getGpa()<<endl;

}
iter = undergrad.end();
iter–;
undergrad.push_front(*iter);
undergrad.pop_back();
cout<<endl<<endl<<setw(70)
<<“Students Roster After last student deleted and inserted at front”<<endl<<endl;
cout<<resetiosflags(ios::right)<<setiosflags(ios::left)<<setw(20)<<“Name”<<setw(10)
<<“SSN”<<setw(10)<<“Year”<<setw(10)<<“Credits”<<resetiosflags(ios::left)
<<setiosflags(ios::right)<<setw(10)<<“Tuition”<<setw(10)<<“GPA”<<endl<<endl;
for( iter = undergrad.begin();iter != undergrad.end();iter++)
{
Undergrad s = *iter;
s.setRate(under_rate);
cout<<resetiosflags(ios::right)<<setiosflags(ios::left)<<setw(20)
<<s.getName()<<setw(10)<<s.getSSN()<<setw(10)<<s.getYear()<<setw(10)
<<s.getCredit()<<resetiosflags(ios::left)<<setiosflags(ios::right)
<<setw(10)<<setfill(‘$’)<<s.tuition()<<setfill(‘ ‘)<<setw(10)<<s.getGpa()<<endl;

}
cout<<endl<<endl<<endl<<endl;
vector<Undergrad>v;
vector<Undergrad>:: iterator it;
for( iter = undergrad.begin();iter != undergrad.end();iter++)
{
v.push_back(*iter);
}
sort(v.begin(),v.end(),sortByGpa);
cout<<setw(70)
<<“Students Roster with students arranged in order of their gpa”<<endl<<endl;
cout<<resetiosflags(ios::right)<<setiosflags(ios::left)<<setw(20)<<“Name”<<setw(10)
<<“SSN”<<setw(10)<<“Year”<<setw(10)<<“Credits”<<resetiosflags(ios::left)
<<setiosflags(ios::right)<<setw(10)<<“Tuition”<<setw(10)<<“GPA”<<endl<<endl;
for(it = v.begin(); it != v.end();it++)
{
Undergrad s = *it;
s.setRate(under_rate);
cout<<resetiosflags(ios::right)<<setiosflags(ios::left)<<setw(20)
<<s.getName()<<setw(10)<<s.getSSN()<<setw(10)<<s.getYear()<<setw(10)
<<s.getCredit()<<resetiosflags(ios::left)<<setiosflags(ios::right)
<<setw(10)<<setfill(‘$’)<<s.tuition()<<setfill(‘ ‘)<<setw(10)<<s.getGpa()<<endl;
}
char ch;
cout<<endl<<endl<<“Please Press enter to exit”;
cin.get(ch);
return 0;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *