In c++ language, I already solved the problem and ran it fine. However, I am not sure if I can use hardcode. I know its unacceptable hardcode. Here’s the question from the exercise. (the Person, Student, Employee, Faculty, and Staff classes) Design a class name Person and its two derived classed named Student and Employee. Make Faculty and Staff derived classes of Employee. A person has a name, address, phone number, and e-mail address. A student has a class status (freshman, sophomore, junior, or senior). An employee has an office, salary, and date-hired. Define a class named MyDate that contains the fields year, month, and day. a faculty member has office hours and a rank. A staff member has a title. Define a constant virtual toString function in the Person class and override it in each class to display the class name and the person’s name. Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString() functions.
Person.h
#ifndef PERSON
#define PERSON
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class Person
{
private:
string status1;
string status2;
string status3;
string status4;
public:
string getStatus()
{
status1 = “Sophomore”;
status2 = “Freshman”;
status3 = “Junior”;
status4 = “Senior”;
return status1, status2, status3, status4;
}
string toString()
{
return “Student”;
}
};
#endif
Faculty.h
#ifndef FACULTY
#define FACULTY
#include <iostream>
#include <cstring>
#include <string>
#include “Person.h”
#include “MyDate.h”
using namespace std;
class Faculty : public MyDate
{
private:
string officeHours;
int rank;
public:
string toString()
{
return “Faculty”;
}
};
#endif
Calendar.h
#ifndef CALENDAR
#define CALENDAR
#include <iostream>
#include <cstring>
#include <string>
#include “Person.h”
#include “MyDate.h”
#include “Faculty.h”
#include “Staff.h”
using namespace std;
class Calendar
{
private:
int year;
int month;
int day;
public:
string toString()
{
return “Calendar”;
}
};
#endif
MyDate.h
#ifndef MYDATE
#define MYDATE
#include <iostream>
#include <cstring>
#include <string>
#include “Person.h”
using namespace std;
class MyDate : public Person
{
protected:
string office;
int salary;
//MyDate dateHired
public:
string toString()
{
return “Employee”;
}
};
#endif
Staff.h
#ifndef STAFF
#define STAFF
#include <iostream>
#include <cstring>
#include <string>
#include “Person.h”
#include “MyDate.h”
#include “Faculty.h”
using namespace std;
class Staff : public MyDate
{
private:
string title;
public:
string toString()
{
return “Staff”;
}
};
#endif
Main.cpp
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include “Triangle.h”
#include “GeometricObject.h”
#include “MyVector.cpp”
#include “Person.h”
#include “MyDate.h”
#include “Faculty.h”
#include “Staff.h”
#include “Calendar.h”
#include “ImprovedMyPoint.h”
#include “ThreeDPoint.h”
#include “NewAccount.h”
#include “CheckingAcc.h”
#include “SavingAcc.h”
#include “MyVector.h”
using namespace std;
//global scope
void Prog15_1();
void Prog15_2();
void Prog15_3();
void Prog15_4();
void Prog15_5();
//to solve a problem #15_2
void Prog15_2()
{
//create the object
Person p;
Staff s1;
Faculty f;
Calendar c;
MyDate m;
//display each class name using toString () function
cout << p.toString() << endl;
cout << s1.toString() << endl;
cout << f.toString() << endl;
cout << c.toString() << endl;
cout << m.toString() << endl;
}
int main()
{
while (true)
{
system(“cls”);
cout << “nMain Menu – Chapter 15” << endl;
cout << “==============================” << endl;
cout << ” 1: Programming Exercise 15.1″ << endl;
cout << ” 2: Programming Exercise 15.2″ << endl;
cout << ” 3: Programming Exercise 15.3″ << endl;
cout << ” 4: Programming Exercise 15.4″ << endl;
cout << ” 5: Programming Exercise 15.5″ << endl;
cout << “other: Exit” << endl;
cout << “==============================” << endl;
cout << “Enter an exercise: “;
char exercise[2];
cin >> exercise;
cout << endl;
switch (atoi(exercise))
{
case 1: Prog15_1(); break;
case 2: Prog15_2(); break;
case 3: Prog15_3(); break;
case 4: Prog15_4(); break;
case 5: Prog15_5(); break;
default: exit(0);
}
cout << endl;
system(“pause”);
cin.clear();
}
return 0;
}
Expert Answer
#include <string>
using std::string;
class MyDate{
int _year;
int _month;
int _day;
public:
MyDate(int year, int month, int day):
_year(year), _month(month), _day(day) {}
MyDate():
_year(0), _month(0), _day(0) {}
};
class Person{
string _name;
string _address;
string _phoneNumber;
string _email;
public:
Person(string name, string address, string phoneNumber, string email):
_name(name), _address(address), _phoneNumber(_phoneNumber), _email(_email){}
Person():
_name(“none”), _address(“none”), _phoneNumber(“none”), _email(“none”) {}
virtual string toString(){
return “Person”;
}
};
class Student : public Person{
enum class_status{frssman,sophomore,junior,senior};
class_status _status;
public:
Student(string name, string address, string phoneNumber, string email, class_status t):
Person(name, address, phoneNumber, email), _status(t) {}
Student() {}
string toString(){
return “Student” ;
}
};
class Employee : public Person{
string _office;
int _salary;
MyDate _dateHired;
public:
Employee(string name, string address, string phoneNumber, string email,
string office, int salary, MyDate dateHired):
Person(name, address, phoneNumber, email),
_office(office), _salary(salary), _dateHired(dateHired) {}
Employee() {}
string toString(){
return “Employee”;
}
};
class Faculty : public Employee{
string _officeHours;
int _rank;
public:
Faculty(string name, string address, string phoneNumber, string email,
string office, int salary, MyDate dateHired,
string officeHours, int rank):
Employee(name, address, phoneNumber, email, office, salary, dateHired),
_officeHours(officeHours), _rank(rank) {}
Faculty() {}
string toString(){
return “Faculty”;
}
};
class Staff: public Employee{
string _title;
public:
Staff(string name, string address, string phoneNumber, string email,
string office, int salary, MyDate dateHired,
string title):
Employee(name, address, phoneNumber, email, office, salary, dateHired),
_title(title) {}
Staff() {}
string toString(){
return “Staff”;
}
};
main.cpp
#include <iostream>
#include <string>
#include “source.h”
using namespace std;
void f1(Person p)
{
cout << p.toString() << endl;
}
void f2(Employee e)
{
cout << e.toString() << endl;
}
void f(Person &p)
{
cout << p.toString() << endl;
}
int main()
{
Person person;
Student student;
Employee employee;
Faculty faculty;
Staff staff;
f1(person);
f1(student);
f1(employee);
f1(faculty);
f1(staff);
f2(employee);
f2(faculty);
f2(staff);
f(person);
f(student);
f(employee);
f(faculty);
f(staff);
return 0;
}