Polymorphism Problem

I have this assign that requires polymorphism, but I have came to an agreement that I do not understand how to implement it. I read different explanations of it, but just can't find out how to make it work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <vector>
#include <iostream>
#include <string>

using namespace std;

//Person Class
class Person
{
  private:
    string name;
    string address;
    string telephone;
    string email;
  
  public:
    Person(string n, string a, string t, string e);
    string getName();
    string getAddress();
    string getTelephone();
    string getEmail();
    virtual string whatami();
    string classify();
};

Person::Person(string n, string a, string t, string e)
{
  name = n;
  address = a;
  telephone = t;
  email = e;
}

string Person::getName()
{
  return name;
}

string Person::getAddress()
{
  return address;
}

string Person::getTelephone()
{
  return telephone;
}

string Person::getEmail()
{
  return email;
}

string Person::whatami()
{
  return "Person";
}

//-----------------------------------------------------

//Student Class
class Student : virtual public Person
{
  private:
    string edu;
  
  public:
    Student(string n, string a, string t, string e, string edu);
    string getEducation();
    virtual string whatami();
    
};

Student::Student(string n, string a, string t, string e, string edu) : Person (n, a, t, e)
{
  this -> edu = edu;
}

string Student::getEducation()
{
  return edu;
}

string Student::whatami()
{
  return "Student";
}

//--------------------------------------------------------

//Employee Class
class Employee : virtual public Person
{
  private:
    string office;
    double salary;
    string hdate;
  
  public:
    Employee(string n, string a, string t, string e, string office, double salary, string hdate);
    string getOffice();
    double getSalary();
    string getHdate();
    virtual string whatami();
};

Employee::Employee(string n, string a, string t, string e, string office, double salary, string hdate) : Person(n, a, t, e)
{
  this -> office = office;
  this -> salary = salary;
  this -> hdate = hdate;
}

string Employee::getOffice()
{
  return office;
}

double Employee::getSalary()
{
  return salary;
}

string Employee::getHdate()
{
  return hdate;
}

string Employee::whatami()
{
  return "Employee";
}

//------------------------------------------------------------
//Faculty Class
class Faculty : public Employee
{
  private:
    string status;
    string rank;
  
  public:
    Faculty(string n, string a, string t, string e, string office, double salary, string hdate, string rank, string status);
    		
    string getRank();
    string getStatus();
    virtual string whatami();
};

Faculty::Faculty(string n, string a, string t, string e, string office, double salary, string hdate, string rank, string status) : Employee(n, a, t, e, office, salary, hdate)
{
  this -> rank = rank;
  this -> status = status;
}

string Faculty::getRank()
{
  return this -> rank;
}

string Faculty::getStatus()
{
  return this -> status;
}

string Faculty::whatami()
{
  return "Faculty";
}

//--------------------------------------------------------------
//Staff Class
class Staff : public Employee
{
  private:
    string job;
    
  public:
    Staff(string n, string a, string t, string e, string office, double salary, string hdate, string job);
    	  
    string getJob();
    virtual string whatami();
};

Staff::Staff(string n, string a, string t, string e, string office, double salary, string hdate, string job) : Employee(n, a, t, e, office, salary, hdate)
{
  this -> job = job;
}

string Staff::getJob()
{
  return job;
}

string Staff::whatami()
{
  return "Staff";
}

//-----------------------------------------------
class StaffST : public Staff, public Student
{
  private:
    int chours;
    
  public:
    StaffST(string n, string a, string t, string e, string office, double salary, string hdate, string job, string edu, int hours);
    
    int getHours();
    virtual string whatami();
};

StaffST::StaffST(string n, string a, string t, string e, string office, double salary, string hdate, string job, string edu, int chours) : Staff(n,a,t,e,office,salary,hdate,job), Student(n,a,t,e,edu)
{
  this -> chours = chours;
}

int StaffST::getHours()
{
  return chours;
}

string StaffST::whatami()
{
  return "StaffST";
}


//--------------------------------------------------------------

string classify(Person *p)
{
  return p->whatami();
}

int main()
{
  vector<Person*> v;
  v.push_back(new Person("John Adams","Boston","617-555-0000","john@adams.com"));
  v.push_back(new Student("John Quincy Adams","Boston","617-555-0000","johnq@adams.com","senior"));
  v.push_back(new Staff("Samuel Adams","Boston","617-555-BEER","sam@adams.com","brewhouse 1",1000,"9-15-1764","Brewer"));
  v.push_back(new StaffST("Samuel Smith","Boston","617-555-BEER","samsmith@adams.com","brewhouse 5",100,"9-15-1774","Taster","junior", 14));
  v.push_back(new Faculty("Austin Powers","England","202-888-SHAG","APowers@powers","MoonBase",250,"06-14-1976","Movie Producer","Tenure"));

  for (int i=0; i<v.size(); i++)
    {
      cout << v[i]->getName() << "  " << classify(v[i]) << endl;
    }

  return 1;
}


This is what comes out...

lab7.cpp: In constructor ‘Faculty::Faculty(std::string, std::string, std::string, std::string, std::string, double, std::string, std::string, std::string)’:
lab7.cpp:152:174: error: no matching function for call to ‘Person::Person()’
 Faculty::Faculty(string n, string a, string t, string e, string office, double salary, string hdate, string rank, string status) : Employee(n, a, t, e, office, salary, hdate)
                                                                                                                                                                              ^
lab7.cpp:152:174: note: candidates are:
lab7.cpp:29:1: note: Person::Person(std::string, std::string, std::string, std::string)
 Person::Person(string n, string a, string t, string e)
 ^
lab7.cpp:29:1: note:   candidate expects 4 arguments, 0 provided
lab7.cpp:11:7: note: Person::Person(const Person&)
 class Person
       ^
lab7.cpp:11:7: note:   candidate expects 1 argument, 0 provided
lab7.cpp: In constructor ‘Staff::Staff(std::string, std::string, std::string, std::string, std::string, double, std::string, std::string)’:
lab7.cpp:187:154: error: no matching function for call to ‘Person::Person()’
 Staff::Staff(string n, string a, string t, string e, string office, double salary, string hdate, string job) : Employee(n, a, t, e, office, salary, hdate)
                                                                                                                                                          ^
lab7.cpp:187:154: note: candidates are:
lab7.cpp:29:1: note: Person::Person(std::string, std::string, std::string, std::string)
 Person::Person(string n, string a, string t, string e)
 ^
lab7.cpp:29:1: note:   candidate expects 4 arguments, 0 provided
lab7.cpp:11:7: note: Person::Person(const Person&)
 class Person
       ^
lab7.cpp:11:7: note:   candidate expects 1 argument, 0 provided
lab7.cpp: In constructor ‘StaffST::StaffST(std::string, std::string, std::string, std::string, std::string, double, std::string, std::string, std::string, int)’:
lab7.cpp:215:199: error: no matching function for call to ‘Person::Person()’
 StaffST::StaffST(string n, string a, string t, string e, string office, double salary, string hdate, string job, string edu, int chours) : Staff(n,a,t,e,office,salary,hdate,job), Student(n,a,t,e,edu)
                                                                                                                                                                                                       ^
lab7.cpp:215:199: note: candidates are:
lab7.cpp:29:1: note: Person::Person(std::string, std::string, std::string, std::string)
 Person::Person(string n, string a, string t, string e)
 ^
lab7.cpp:29:1: note:   candidate expects 4 arguments, 0 provided
lab7.cpp:11:7: note: Person::Person(const Person&)
 class Person
       ^
lab7.cpp:11:7: note:   candidate expects 1 argument, 0 provided
Last edited on
Wait... I think I solved my own problem. I'm missing the default constructor in the Person class.
Topic archived. No new replies allowed.