In this post l just want to write a simple program to display usage of inheritance and polymorphism in C++.
I’ve included comments in the code to help understand what is going on and l hope this is useful to someone.

#include <iostream>

using namespace std;
#include <iomanip>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
// BASE CLASS STUDENT DEFINES THE GENERAL DATA MEMBERS AND FUNCTIONS OF THE CLASS
class Student
{
public://all member functions here are accessible everywhere. they are the utility services of both base and derived classes
Student *nxt;//POINTER TO THE STUDENT CLASS, THIS IS NEEDED FOR THE LIST CONTAINER
virtual float tuition() =0;//PURE VIRTUAL FUNCION, NEEDS TO BE OVERRIDEN BY EACH DERIVED CLASS

Student(const string &,const string &,const string &, int ,float);//CONSTRUCTOR FOR THE CLASS

void setName(const string &sname)
{
name = sname;
}
void setSSN(const string &sssn)
{
ssn = sssn;
}
void setYear(const string &syear)
{
year = syear;
}
string getName()
{
return name;
}
string getSSN()
{
return ssn;
}
string getYear()
{
return year;
}
int getCredits(int credit)
{
if (credit > 0)
{
credits = credit;
}
else
{
cout<<“The credit can not be less or equal to zero”<<endl;
cout<<“Enter a positive integer to continue:”;
cin>>credit;
credits = credit;
}
return credits;
}
float getGpa()
{
return gpa;
}

virtual void print()const//VIRTUAL FUNCTION THAT CAN BE OVERRIDEN BY THE DCL. IT DOES THE PRINTING WORK.
{
cout<<setw(20)<<“Student Name:”<<name<<endl;
cout<<setw(20)<<“Social Security:”<<ssn<<endl;
cout<<setw(20)<<“Year:”<<year<<endl;
cout<<setw(20)<<“Credit:”<<credits<<endl;
cout<<setw(20)<<“GPA:”<<gpa<<endl;
}
protected://All data declared here is accessible to the base class’s member functions and that of the derived classes’s.
string name;//stores the name of a student
string ssn;//stores the social security number of a student
string year;//stores the students year i.e freshman,sophomore,junior,senior etc
float gpa;//stores the student’s gpa
int credits;//stores the number of credits a student have

};

//CLASS UNDERGRAD DERIVED FROM THE BASE CLASS STUDENTS
class Undergrad : public Student
{
public:

Undergrad(const string sname, const string sssn, const string syear,int credits,float gpa):
Student(sname,sssn, syear,credits,gpa)
{
Undergrad_rate = 400.0;
tuition();
}

virtual float tuition()
{
return Undergrad_rate * credits;
}
virtual void print()
{
print();
cout<<setw(20)<<“Tuition:”<<tuition()<<endl;
}
private:
float Undergrad_rate;
};
class Grad:public Student
{
public:
Grad(const string sname,const string sssn,const string syear,int credits, float gpa, string &ptr):
Student(sname,sssn,syear,credits,gpa)
{
grad_rate = 500.0;
tuition();
thesis = &ptr;
}
virtual float tuition()
{
return grad_rate * credits;
}
virtual void print()
{
print();
cout<<setw(20)<<“Tuition:”<<tuition()<<endl;
cout<<setw(20)<<“Thesis :”<<thesis<<endl;
}
private:
float grad_rate;
string *thesis;
};
int main()

{
Undergrad under(“Mary”,”000111222″,”Junior”,12,4.0 );
under.print();
Grad grad(“David”,”111222333″,”Graduate”,9,3.7,”How to learn data structures using C++/STL?”);
grad.print();
Grad ngrad(“Jason”,”222333444″,”Graduate”,9,3.9,”Design of efficient algorithms”);
ngrad.print();

list<Student> students;
students.push_back(under);
students.push_back(grad);
students.push_back(ngrad);
list<Student> temp;
nxt = &(students.end());
temp.assign(*nxt);
students.push_front(temp);
delete temp;
cout<<setw(10)<<“Name”<<setw(10)<<“SSN”<<setw(10)<<“Year”<<setw(10)<<“Credits”<<setw(10)<<“GPA”<<endl;
void print(list<Student>::iterator j );
{
cout<< *j <<endl;
}

for(list<Student>:: iterator i = students.begin(); i !=students.end();i++)
{
print(i);

}
vector <Student> v;

}

Leave a Reply

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

Name *