This looks like an assignment from the same class I am in and I am having trouble as well.
1. Create a new class StaffST which has Staff and Student as base classes. StaffSt has an additional attribute (int) credithours.
2. Create the virtual string function whatami() for the Person class. The function returns the type of class - Person, Faculty, Staff, etc. Note that you must create a separate function for each derivedclass.
3. Write a string function classify which accepts a Person pointer variable as an argument and returns the output from whatami().
4. Write a test main program that creates a vector of pointers to Person. Creat a Person, Student, Employee, Faculty, Staff and StaffST object and add pointers to these objects to your vector. Now use the function classify to output the type of each object from the vector pointers.
I am having trouble with a couple of things:
In part 1 I get " error: ‘Person’ is an ambiguous base of ‘StaffST’ "
I think I got part 2 but since I cannot seem to figure out how to create the classify function I cannot check that
In part 3 I have no idea how or where to write the classify function so any help would be appreciated
This is my code:
#ifndef PERSON_H
#define PERSON_H
#include <string>
using namespace std;
class Person{
public:
Person();
Person(string n, string a, string p, string e);
string getname(){return name;}
string getaddress(){return address;}
string getphone(){return phone;}
string getemail(){return email;}
virtual string whatami(){return "Person";}
private:
string name;
string address;
string phone;
string email;
};
Person::Person(){
name = "name";
address = "address";
phone = "phone";
email = "email";
}
Person::Person(string n, string a, string p, string e){
name = n;
address = a;
phone = p;
email = e;
}
#endif
#ifndef STUDENT_H
#define STUDENT_H
#include "Person.h"
#include <string>
using namespace std;
class Student : public Person{
public:
Student();
Student(string n, string a, string p, string e, string i, string y);
string getid(){return id;}
string getyear(){return year;}
virtual string whatami(){return "Student";}
private:
string id;
string year;
};
Student::Student():Person(){
id = "id";
year = "year";
}
Student::Student(string n, string a, string p, string e, string i, string y):Person(n, a, p, e){
id = i;
year = y;
}
#endif
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include "Person.h"
#include <string>
using namespace std;
class Employee : public Person{
public:
Employee();
Employee(string n, string a, string p, string e, string o, string s, string h);
string getoffice(){return office;}
string getsalary() {return salary;}
string gethiredate() {return hiredate;}
virtual string whatami(){return "Employee";}
private:
string office;
string salary;
string hiredate;
};
Employee::Employee():Person(){
office = "office";
salary = "salary";
hiredate = "hiredate";
}
Employee::Employee(string n, string a, string p, string e, string o, string s, string h):Person(n, a, p, e){
office = o;
salary = s;
hiredate = h;
}
#endif
#ifndef STAFF_H
#define STAFF_H
#include "Person.h"
#include "Employee.h"
#include <string>
using namespace std;
class Staff : public Employee{
public:
Staff();
Staff(string n, string a, string p, string e, string o, string s, string h, string pos);
string getposition(){return position;}
virtual string whatami(){return "Staff";}
private:
string position;
};
Staff::Staff(){
position = "position";
}
Staff::Staff(string n, string a, string p, string e, string o, string s, string h, string pos):Employee(n, a, p, e, o, s, h){
position = pos;
}
#endif
#ifndef FACULTY_H
#define FACULTY_H
#include "Person.h"
#include "Employee.h"
#include <string>
using namespace std;
class Faculty : public Employee{
public:
Faculty();
Faculty(string n, string a, string p, string e, string o, string s, string h, string r, string st);
string getrank(){return rank;}
string getstatus(){return status;}
virtual string whatami(){return "Faculty";}
private:
string rank;
string status;
};
Faculty::Faculty():Employee(){
rank = "rank";
status = "status";
}
Faculty::Faculty(string n, string a, string p, string e, string o, string s, string h, string r, string st):Employee(n, a, p, e, o, s, h){
rank = r;
status = st;
}
#endif
#ifndef STAFFST_H
#define STAFFST_H
#include "Person.h"
#include "Student.h"
#include "Employee.h"
#include "Staff.h"
#include <string>
using namespace std;
class StaffST : public Student, public Staff{
public:
StaffST();
StaffST(string n, string a, string p, string e, string i, string y, string o, string s, string h, string pos, int ch);
int getcredithours(){return credithours;}
virtual string whatami(){return "StaffST";}
private:
int credithours;
};
StaffST::StaffST(){
credithours = 0;
}
StaffST::StaffST(string n, string a, string p, string e, string i, string y, string o, string s, string h, string pos, int ch):Student(n, a, p, e, i, y),Staff(n, a, p, e, o, s, h, pos){
credithours = ch;
}
#endif
int main(){
vector<Person*> v;
v.push_back(new Person("John Adams","Boston","617-555-0000","
[email protected]"));
v.push_back(new Student("John Quincy Adams","Boston","617-555-0000","
[email protected]","0098765","senior"));
v.push_back(new Employee("John Smith","Boston","617-555-0000","
[email protected]","brewhouse 2","1000","9-15-1759"));
v.push_back(new Staff("Samuel Adams","Boston","617-555-BEER","
[email protected]","brewhouse 1","1000","9-15-1764","Brewer"));
v.push_back(new Faculty("John Samuel Adams","Boston","617-555-BEER","
[email protected]","brewhouse 3","1000","9-15-1769","Assistant","Brewer"));
v.push_back(new StaffST("John Adams Smith","Boston","617-555-BEER","
[email protected]","0012345","junior","brewhouse 5","100","9-15-1774","Taster",15));
for (int i=0; i<v.size(); i++){
cout << classify(v[i]) << endl;
cout << "Name: " << v[i]->getname() << endl;
cout << "Address: " << v[i]->getaddress() << endl;
cout << "Phone: " << v[i]->getphone() << endl;
cout << "Email: " << v[i]->getemail() << endl;
if(classify(v[i]) == "Student"){
cout << "ID: " << v[i]->getid() << endl;
cout << "Year: " << v[i]->getyear() << endl;
}
if(classify(v[i]) == "Employee"){
cout << "Office: " << v[i]->getoffice() << endl;
cout << "Salary: " << v[i]->getsalary() << endl;
cout << "Hire Date: " << v[i]->gethiredate() << endl;
if(classify(v[i]) == "Staff"){
cout << "Position: " <<v[i]->getposition() << endl;
}
if(classify(v[i]) == "Faculty"){
cout << "Rank: " << v[i]->getrank() << endl;
cout << "Staff: " << v[i]->getstaff() << endl;
}
}
if(classify(v[i]) == "StaffST"){
cout << "Credit Hours: " << v[i]->getcredithours() << endl;
}
cout << endl;
}
return 1;
}