I have an assignment which deals with inheritance.
I have a Base Class of Employee(), which Manager(), and Intern() inherit from.
My instructor gave us the following instruction:
In main() declare an array of staff pointers and initialize them to the following records:
Manager("IT", 100, "MF1", "ML1")
Manager("HR", 50, "MF2", "ML2")
Intern("SIUE", 0, "IF1", "IL1")
Intern("SLU", 0, "IF2", "IL2")
Then i have to loop through and display the array. The output he provides as an example shows that the Manager() and Intern() toString() methods must have been called, as specific information related to the child classes was ouput. However, when accessing the array, im getting employee pointers, not pointers to the child. I've attached the screenshot he provided.
I'm not sure how i'm supposed to accomplish this.
Here's my staff array declaration:
Employee * staff = new Employee[4];
I can't store a pointer to the Manager() object i don't think. Unless there is someway i can fan-dangle this:
staff[0] = new Manager("IT", 100, "MF1", "ML1");
It seems my only option is doing something like this(how would i do this in one line btw?):
Employee * e = new Manager("IT", 100, "MF1", "ML1");
staff[0] = *e;
But obviously now when i do this:
cout << staff[0].toString();
I'm going to get the base class Employee() toString() method, not the child class.
Here are my following two classes definitions for Employee() and Manager() in case it's relevant:
class Employee{
private:
int eid;
string firstName;
string lastName;
public:
Employee();
Employee(int eid, string fname, string lname);
void setEID(int eid);
void setFirstName(string fname);
void setLastName(string lname);
int getEID();
string getFirstName();
string getLastName();
virtual string toString();
};
class Manager : public Employee, IPayable{
private:
string department;
public:
Manager();
Manager(string dept, int eid, string fname, string lname);
double pay();
void setDepartment(string d);
string getDepartment();
string toString();
};
And here is how i assumed i would implement the 2 toString() methods, with the Employee toString() embedded within the Manager() toString():
string Employee::toString(){
return "{eid = " + std::to_string(getEID()) + ", firstName = " + getFirstName() + ", lastName = " + getLastName() + "}";
}
string Manager::toString(){
return "Manager{" + Employee::toString() + ", department = " + getDepartment() + ", salary = " + std::to_string(pay()) +"}";
}
What you want is an array of pointers:
Employee *staff[4];
Now you can assign pointers to Employee, and therefore pointers to children of Employee to it:
staff[0] = new Manager("IT", 100, "MF1", "ML1");
And then you can call functions of Employee :
staff[0]->toString();
Here is one way:
Manager manager1("IT", 100, "MF1", "ML1");
Manager manager2("HR", 50, "MF2", "ML2");
Intern intern1("SIUE", 0, "IF1", "IL1")
Intern intern2("SLU", 0, "IF2", "IL2")
Employee *staff[4] = {
&manager1,&manager2,&intern1,&intern2
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With