I have a base class Employee, and two derived classes FullTimeEmp and PartTimeEmp.
I used an stl container - vector this way....
vector<Employee> workers;
I use 'workers' to store all, that is FullTimeEmp, PartTimeEmp and Employee. When I access them through workers.at(i) and try to call a virtual method in the classes- getPay()...it doesn't work. It keeps calling the Employee getPay() . I want it to call each differently depending on whether its a PartTimeEmp or FullTimeEmp accessed. Help! How do I achieve polymorphism?
example:
class Employe {....);
class FullTimeEmpublic Employee {};
class PartTimeEmpublic Employee {};
vector<Employee> emps;
FullTimeEmp fte(....);
PartTimeEmp pte(...);
emps.push_back(fte);
emps.push_back(pte);
double fulltimeEmppay = emps.at(0).getPay();
double parttimeEmppay = emps.at(1).getPay();
//so the getPay() method isn't polymorphic...how can I makeit polymorphic?
I have the method getPay() as virtual in baseclass Employee and have overriden it well in the derived classes.
Jairus
I used an stl container - vector this way....
vector<Employee> workers;
I use 'workers' to store all, that is FullTimeEmp, PartTimeEmp and Employee. When I access them through workers.at(i) and try to call a virtual method in the classes- getPay()...it doesn't work. It keeps calling the Employee getPay() . I want it to call each differently depending on whether its a PartTimeEmp or FullTimeEmp accessed. Help! How do I achieve polymorphism?
example:
class Employe {....);
class FullTimeEmpublic Employee {};
class PartTimeEmpublic Employee {};
vector<Employee> emps;
FullTimeEmp fte(....);
PartTimeEmp pte(...);
emps.push_back(fte);
emps.push_back(pte);
double fulltimeEmppay = emps.at(0).getPay();
double parttimeEmppay = emps.at(1).getPay();
//so the getPay() method isn't polymorphic...how can I makeit polymorphic?
I have the method getPay() as virtual in baseclass Employee and have overriden it well in the derived classes.
Jairus
Last edited: