Polymorphism

P

Pieter

Hello,


//I have the following situation:

//baseclass : Employee

class Employee{

private:
string name;

public :
Employee(string);
void SetName(string);
virtual double Earnings();

//********
derived classes: Manager , Salesman
//a Manager gets paid hourly ( a constant Wage),
//a Salesman gets a fixed wage + a Commission

class Manager : public Employee {

private:
int hours;
static double wage;

public:
Manager(string);
void getHours(int); // <-----
virtual double Earnings();


/*PROBLEM:

All the managers and salesmen are stored in a STL map
map<int,Employee> ieMap; in an other class.

When I iterate this map, how can ik call getHours(int) from Manager
(supposed the iterator points to a Manager object,
or is it the whole "point" that the objects Manager and Salesman are stored
as an Employee???? I hope not..).

I tried RTTI, didn't work (probably my fault, but hey, I couldn't do it)

Is there a possibility with Polymorphism??? (and cleaner..)

Hints, tips??

kind regards,
Pieter

ps: sorry for my bad "inglisch";)


*/
 
V

Victor Bazarov

Pieter said:
//I have the following situation:

//baseclass : Employee

class Employee{

private:
string name;

public :
Employee(string);
void SetName(string);
virtual double Earnings();

//********
derived classes: Manager , Salesman
//a Manager gets paid hourly ( a constant Wage),
//a Salesman gets a fixed wage + a Commission

class Manager : public Employee {

private:
int hours;
static double wage;

public:
Manager(string);
void getHours(int); // <-----
virtual double Earnings();


/*PROBLEM:

All the managers and salesmen are stored in a STL map
map<int,Employee> ieMap; in an other class.

When I iterate this map, how can ik call getHours(int) from Manager
(supposed the iterator points to a Manager object,
or is it the whole "point" that the objects Manager and Salesman are stored
as an Employee???? I hope not..).

You cannot. If you store _objects_, you get the situation known in C++
as _slicing_. Every time you get an instance of Manager and try to stuff
it into your map, the Employee part of the Manager gets stored and the
rest gets _sliced__off_.
I tried RTTI, didn't work (probably my fault, but hey, I couldn't do it)

Is there a possibility with Polymorphism??? (and cleaner..)

Hints, tips??


You need to store pointers to Employee:

map<int,Employee*>

but make sure you create your Managers or Salesmen _dynamically_ and
dispose of them properly when you don't need them any longer. Also,
in order to do that, your Employee's destructor _must_ be virtual.

Search Google Groups for "polymorphism STL container" for more
information (no double quotes, of course).

Victor
 
D

David White

Victor Bazarov said:
You need to store pointers to Employee:

map<int,Employee*>

but make sure you create your Managers or Salesmen _dynamically_ and
dispose of them properly when you don't need them any longer.

I agree that for most practical purposes you would have to create the
objects dynamically, but it's not a requirement as far as the map is
concerned. You could do something like this if you wanted:

int main()
{
// create a bunch of automatic Managers and Salesman objects
// (but not in a loop or nested scope)

// create the map

// add the addresses of all Employees to the map

// iterate over the map and do various polymorphic operations
// or pass the map to other functions

} // everything destroyed automatically

I just wanted to make sure the OP didn't misunderstand the reason for
creating the objects dynamically, which is that normally you wouldn't know
what Employee objects you'll need until run-time.

DW
 
C

Cy Edmunds

Pieter said:
Hello,


//I have the following situation:

//baseclass : Employee

class Employee{

private:
string name;

public :
Employee(string);
void SetName(string);
virtual double Earnings();

//********
derived classes: Manager , Salesman
//a Manager gets paid hourly ( a constant Wage),
//a Salesman gets a fixed wage + a Commission

class Manager : public Employee {

private:
int hours;
static double wage;

public:
Manager(string);
void getHours(int); // <-----
virtual double Earnings();


/*PROBLEM:

All the managers and salesmen are stored in a STL map
map<int,Employee> ieMap; in an other class.

When I iterate this map, how can ik call getHours(int) from Manager
(supposed the iterator points to a Manager object,
or is it the whole "point" that the objects Manager and Salesman are stored
as an Employee???? I hope not..).

I tried RTTI, didn't work (probably my fault, but hey, I couldn't do it)

Is there a possibility with Polymorphism??? (and cleaner..)

Hints, tips??

kind regards,
Pieter

ps: sorry for my bad "inglisch";)


*/

See the thread:

HELP: vector & polymorphism

The situation with std::map is the same.

Newsgroup: Can we PLEASE get this into the FAQ? It's bad enough that the
standard library support for polymorphism is so thin -- the workarounds need
to be documented.
 

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,781
Messages
2,569,616
Members
45,305
Latest member
KetoMeltsupplement

Latest Threads

Top