In: Computer Science
C++ Programming.
Create a class hierarchy to be used in a university setting. The classes are as follows:
The base class isPerson. The class should have 3 data members: personID (integer), firstName(string), and lastName(string). The classshould also have a static data member called nextID which is used to assignanID numbertoeach object created(personID). All data members must be private.
Create the following member functions: Accessor functions to allow access to first name and last name (updates and retrieval). These functions should be public. Create a getID function to retrieve the personID of the person. This function should be public. The user oftheclassor any derived class should not have the ability to update personID. Create a constructor with two arguments(first name and last name) with default values.
Create a public member function Print to display information stored in the object. Make this class an abstract class. Create theStudent class as a derived class from the Person class. This class should have two additional data members to keep track of astudent’s GPA (float) and admission term (string –eg. Fall 2019). Create public accessor functions to allow access to the GPA and admission term. The setGPA function should return true if the value of the GPA is between 0 and 4.0(inclusive) and false otherwise. Set the GPA to zero if an invalid value is provided. Override the function Print to print the student information. Create a constructor that takes in four argumentswith default values(first name, last name, admission term, and GPA). Create the Faculty class as a derived class from the Person class. This class should have one additional data member to keep track of the yearthe faculty was hired(string –eg. 2019 ) Create a public accessor functionto allowaccess to the hire year. Override the function Print to print the faculty information. Create a constructor that takes in three argumentswith default values(first name, last name, hire year).
After defining the classes in this hierarchy, write a program that creates objects of each class and tests their member functions. Make sure you include the preprocessor directives (#ifndef, #define, #endif) to prevent a header file from being included (#include) multiple times. Separate the class interface from the class implementation.
Output Sample:
Faculty First Name: John
Faculty Last Name:Smith
Faculty ID:5
Faculty Hire Year:2019
******************************************************
******************************************************
Student First Name: Sarah
Student Last Name: Smith
Student ID: 3
Student GPA: 3.75
Student Admit Term: Fall 2019
CODE:
//Tester program for Person, Student and Faculty classes
//main.cpp
#include<iostream>
//include Student and Faculty header files
#include "Student.h"
#include "Faculty.h"
using namespace std;
int main()
{
//Create Student and faculty class objects
Student std("John","Smith",3.75,"Spring 2019");
Faculty faculty("Mary","Smith","2019");
cout<<"*******************************************\n";
//calling Print method on faculty
faculty.Print();
cout<<"*******************************************\n";
cout<<"\n\n\n";
cout<<"*******************************************\n";
//calling Print method on Student object
std.Print();
cout<<"*******************************************\n";
return 0;
}
-----------------------------------------------------------------------------------------
//Person.h
#ifndef PERSON_H
#define PERSON_H
#include<iostream>
#include<string>
using namespace std;
class Person
{
private:
int personID;
string firstName;
string lastName;
public:
//Constructor and methods
Person(string fName, string lName);
string getFirstName();
string getLastName();
int getID();
virtual void Print() = 0;
static int nextID;
};
#endif //PERSON_H
-----------------------------------------------------------------------------------------
//Person.cpp
#include<iostream>
#include "Person.h"
using namespace std;
//Set static variable value nextID to 0
int Person::nextID = 0;
//Implementation of methods of Person class
Person::Person(string fName, string lName)
{
nextID=nextID+1;
personID=nextID;
this->firstName=fName;
this->lastName=lName;
}
string Person::getFirstName()
{
return firstName;
}
string Person::getLastName()
{
return lastName;
}
int Person::getID()
{
return personID;
}
-----------------------------------------------------------------------------------------
//Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include<iostream>
#include<string>
#include "Person.h"
using namespace std;
class Student : public Person
{
private:
float gpa;
string admissionTerm;
public:
Student(string fName, string lName, float gpa, string
adTerm);
bool setGPA(float gpa);
float getGPA();
string getAdmissionTerm();
void Print();
};
#endif //STUDENT_H
-----------------------------------------------------------------------------------------
//Student.cpp
#include<iostream>
#include<iomanip>
#include "Student.h"
#include<string>
using namespace std;
Student::Student(string fName, string lName, float gpa, string
adTerm): Person(fName, lName)
{
setGPA(gpa);
admissionTerm=adTerm;
}
bool Student::setGPA(float gpa)
{
if(gpa<0 || gpa>4)
{
gpa=0;
return false;
}
else
this->gpa=gpa;
}
float Student::getGPA()
{
return gpa;
}
string Student::getAdmissionTerm()
{
return admissionTerm;
}
void Student::Print()
{
cout<<fixed<<setprecision(2);
cout<<left<<setw(20)<<"Student First
Name "
<<":"<<setw(20)<<getFirstName()<<endl;
cout<<left<<setw(20)<<"Student Last
Name "
<<":"<<setw(20)<<getLastName()<<endl;
cout<<left<<setw(20)<<"Student ID
"
<<":"<<setw(20)<<getID()<<endl;
cout<<left<<setw(20)<<"Student GPA
"
<<":"<<setw(20)<<getGPA()<<endl;
cout<<left<<setw(20)<<"Student Admit
Term "
<<":"<<setw(20)<<getAdmissionTerm()<<endl;
}
-----------------------------------------------------------------------------------------
//Faculty.h
#ifndef FACULTY_H
#define FACULTY_H
#include<iostream>
#include<string>
#include "Person.h"
using namespace std;
class Faculty : public Person
{
private:
string year;
public:
Faculty(string fName, string lName,string year);
string getYear();
void Print();
};
#endif //FACULTY_H
-----------------------------------------------------------------------------------------
//Faculty.cpp
#include<iostream>
#include<iomanip>
#include "Faculty.h"
#include<string>
using namespace std;
//Implementation of methods of Person class
Faculty::Faculty(string fName, string lName, string year) :
Person(fName, lName)
{
this->year=year;
}
string Faculty::getYear()
{
return year;
}
void Faculty::Print()
{
cout<<fixed<<setprecision(2);
cout<<left<<setw(20)<<"Faculty First
Name "
<<":"<<setw(20)<<getFirstName()<<endl;
cout<<left<<setw(20)<<"Faculty Last
Name "
<<":"<<setw(20)<<getLastName()<<endl;
cout<<left<<setw(20)<<"Faculty ID
"
<<":"<<setw(20)<<getID()<<endl;
cout<<left<<setw(20)<<"Faculty Hire
Year "
<<":"<<setw(20)<<getYear()<<endl;
}
-----------------------------------------------------------------------------------------
OUTPUT: