C ++ Inheritance - how to access child methods with a pointer to an object of a base class

I have an assignment that deals with inheritance.

I have an Employee () base class that inherits from Manager () and Intern ().

My instructor gave us the following instructions:

In main (), declare an array of pointers to personnel and initialize them with the following entries:

Manager("IT", 100, "MF1", "ML1")
Manager("HR", 50, "MF2", "ML2")
Intern("SIUE", 0, "IF1", "IL1")
Intern("SLU", 0, "IF2", "IL2")

Then I need to scroll and display the array. The result, which he provides as an example, shows that the Manager () and Intern () toString () methods should have been called because there was some information related to the child classes. However, when accessing the array, im getting pointers to employees, not pointers to a child. I added a screenshot that he provided.

Screenshot of what my output needs to look like

I am not sure how I should do this.

:

Employee * staff = new Employee[4];

Manager(), . -, :

staff[0] = new Manager("IT", 100, "MF1", "ML1");

, - - ( ?):

Employee * e = new Manager("IT", 100, "MF1", "ML1");
staff[0] = *e;

, , , :

cout << staff[0].toString();

Employee() toString(), .

Employee() Manager() , :

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();
};

, 2 toString(), Employee toString(), 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()) +"}";
}
+5
5

- :

Employee *staff[4];

Employee , , Employee:

staff[0] = new Manager("IT", 100, "MF1", "ML1");

Employee:

staff[0]->toString();
+3

:

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
};
+2
typedef std::unique_ptr<Employee> EmployeePtr;
typedef std::array<EmployeePtr,4> Employees;
Employees employees =
{
    new Manager("IT", 100, "MF1", "ML1"),
    new Manager("HR", 50, "MF2", "ML2"),
    new Intern("SIUE", 0, "IF1", "IL1"),
    new Intern("SLU", 0, "IF2", "IL2")
};

Stored. ? .

employees[0]->toString();

.

+2

Manager, ,

, . , , Employee*, Manager. , Manager Employees ( , ), .

toString() virtual, Manager , Manager Employee* Manager .

, ( ):

staff[0] = *e;

. :

staff[0] = new Manager(...);
...
staff[0]->toString();

, raw-, delete , , . , .

+1

, , , .

std::vector<Employee*> staff;

staff.push_back(new Manager);
staff.push_back(new Intern);

, Employee. toString() Employee , - Manager Intern.

staff[0]->toString(); // Manager toString()
staff[1]->toString(); // Intern toStrin()

, !

+1

All Articles