stl container- vector + polymorphism

Joined
Nov 16, 2006
Messages
5
Reaction score
0
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 FullTimeEmp:public Employee {};
class PartTimeEmp:public 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:
Joined
Nov 16, 2006
Messages
5
Reaction score
0
I guess I solved my own problem....is there anyone out there?

so I should've used vector<Shape *> emps;

then call the virtual method using... emps.at(i)->getPay();

That gives it the polymorphic feeling......!

thanks anyways...everyone who viewed this post!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top