Need help on some homework!

Hi there!

I am in need of some assistance on a homework problem. I am writing some code that involves inheritance and polymorphism that asks the following questions:

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 derived class.

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 have written all my classes and have done questions 1 and 2, but the thing that I am currently stuck on is question number 3. I am confused how to write the function "classify". Like how do I make it point to person and return whatami()?

Here's the person class code:
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
#include <string>
using namespace std;  

class Person
{
public:
    Person();
    Person(string nm, string addr, string pnumber, string emaddr);
    string getName();
    string getAddress();
    string getPhoneNumber();
    string getEmailAddress();
    virtual string whatami();
private:
    string name;
    string address;
    string phonenumber;
    string emailaddress;
};

Person::Person()
{
    name = "";
    address = "";
    phonenumber = "";
    emailaddress = "";
}

Person::Person(string nm, string addr, string pnumber, string emaddr)
{
    name = nm;
    address = addr;
    phonenumber = pnumber;
    emailaddress = emaddr;
}

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

string Person::getEmailAddress()
{
    return emailaddress;
}

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

string Person::getPhoneNumber()
{
    return phonenumber;
}

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



Person is the base class that all the other classes inherit. Well, class Student and class Employee inherit Person directly, and classes Faculty and Staff inherit Employee. The new class, StaffST inherits both Student and Staff.

That's the basis of the program, would post the entire thing but it would be quite lengthy, and really each class looks just like the person class but only have their corresponding names in the place of person.
Last edited on
Simply:
- The function needs to have one parameter: a pointer to a person.
- Inside the function, call the whatami() function.
- Return that value.
Is that helpful?
Some what... though I am still unsure what the point to a person is suppose to be pointing too exactly... Maybe I am overthinking this.

Would it be something like this?

1
2
3
4
5
string Person::classify(//pointer to Person)
{
     whatami();
     return whatami();
}
I believe it would be:
1
2
3
4
string Person::classify(Person *ptr)
{
    return ptr->whatami();
}
Alright that does work but I am running into an issue in my main program... When I try to compile it I am getting an error that classify was not declared in this scope for every time is declared. Wondering why it's not seeing it?

Here's my int main:

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
#include <iostream>
#include <string>
#include <vector>
#include "person.h"
#include "student.h"
#include "faculty.h"
#include "employee.h"
#include "staff.h"
#include "staffst.h"

using namespace std;


int main()
{
    vector<Person*> v;
    v.push_back(new Person("John Doe", "1234 Unknown Rd", "123-456-7890", "johndoe@gmail.com"));
    v.push_back(new Student("Jane Doe", "4321 Unknown Ave", "098-765-4321", "janedoe@gmail.com", "Junior"));
    v.push_back(new Staff("John Smith", "3214 Unknown Blvd", "456-123-7890", "johnsmith@gmail.com", "23", "$1234", "01/01/2012", "Janitor"));
    v.push_back(new Staffst("John Adam", "2143 Unknown Ln", "654-321-0987", "johnadam@gmail.com", "41", "$3214", "06/05/2012", "Instructor", 100));
    v.push_back(new Faculty("Jane Smith", "2134 Unknown St", "789-456-1230", "janesmith@gmail.com", "32", "$4321", "12/31/2012", "Professor", "Tenured"));
    
    for (int i=0; i<v.size(); i++)
    {
        cout << "Name: " << v[i]->getName() << endl;
        cout << "Address: " << v[i]->getAddress() << endl;
        cout << "Phone#: " << v[i]->getPhoneNumber() << endl;
        cout << "Email Address: " << v[i]->getEmailAddress() << endl;
        
        if(classify(v[i]) == "Employee")
        {
            cout << "Office: " << v[i]->getOffice() << endl;
            cout << "Salary: " << v[i]->getSalary() << endl;
            cout << "Date Hired: " << v[i]->getDateHired() << endl;
        }
        if(classify(v[i]) == "Faculty")
        {
            cout << "Rank: " << v[i]->getRank() << endl;
            cout << "Status: " << v[i]->getStatus() << endl;
        }
        if(classify(v[i]) == "Staff")
        {
            cout << "Position: " << v[i]->getPosition() << endl;
        }
        if(classify(v[i]) == "Staffst")
        {
            cout << "Credit Hours: " << v[i]->getCreditHours() << endl;
        }
    }
    
    return 1;
}  
I don't think classify() should be a member of person.
So should it be a member of all the other classes except person?
It seems like they want it to be a global function. Don't make it a member of any class.
Ah! That does make perfect sense, I'll try it.
Alright that did indeed get rid of the "classify was not declared in this scope" error, but now have this...


maintest.cpp: In function 'int main()':
maintest.cpp:36: error: 'class Person' has no member named 'getOffice'
maintest.cpp:37: error: 'class Person' has no member named 'getSalary'
maintest.cpp:38: error: 'class Person' has no member named 'getDateHired'
maintest.cpp:42: error: 'class Person' has no member named 'getRank'
maintest.cpp:43: error: 'class Person' has no member named 'getStatus'
maintest.cpp:47: error: 'class Person' has no member named 'getPosition'
maintest.cpp:51: error: 'class Person' has no member named 'getCreditHours'


It's apparently not testing the if statements and just reading purely out of class Person?
Your vector contains pointers to Person, and you are trying to call methods that aren't a member of it. You would need to make those methods virtual (but possibly undefined) in Person in order for the program to work as you've written it.

The compiler reads the code before the vector and the entries even exist, so it is assuming you have pointers to People, and it is failing because the methods you reference only exist in the more derived classes.
Well I followed your advice, added the virtual functions as you said and it did all compile and thought I had it but then I got an unfortunate long list of stuff when I tried to compile it all together.


Undefined symbols for architecture x86_64:
"Person::getPosition()", referenced from:
vtable for Facultyin faculty.o
construction vtable for Employee-in-Facultyin faculty.o
vtable for Employeein employee.o
vtable for Studentin student.o
vtable for Personin person.o
construction vtable for Employee-in-Staffin staff.o
construction vtable for Student-in-Staffstin staffst.o
...
"Person::getDateHired()", referenced from:
vtable for Studentin student.o
vtable for Personin person.o
construction vtable for Student-in-Staffstin staffst.o
"Person::getCreditHours()", referenced from:
vtable for Facultyin faculty.o
construction vtable for Employee-in-Facultyin faculty.o
vtable for Employeein employee.o
vtable for Studentin student.o
vtable for Personin person.o
vtable for Staffin staff.o
construction vtable for Employee-in-Staffin staff.o
...
"Person::getOffice()", referenced from:
vtable for Studentin student.o
vtable for Personin person.o
construction vtable for Student-in-Staffstin staffst.o
"Person::getSalary()", referenced from:
vtable for Studentin student.o
vtable for Personin person.o
construction vtable for Student-in-Staffstin staffst.o
"Person::getStatus()", referenced from:
construction vtable for Employee-in-Facultyin faculty.o
vtable for Employeein employee.o
vtable for Studentin student.o
vtable for Personin person.o
vtable for Staffin staff.o
construction vtable for Employee-in-Staffin staff.o
vtable for Staffstin staffst.o
...
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Topic archived. No new replies allowed.