Discussion:
Polymorphism
(too old to reply)
Pieter
2003-12-23 15:21:43 UTC
Permalink
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";)


*/
Victor Bazarov
2003-12-23 15:44:02 UTC
Permalink
Post by Pieter
//baseclass : Employee
class Employee{
string name;
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 {
int hours;
static double wage;
Manager(string);
void getHours(int); // <-----
virtual double Earnings();
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_.
Post by Pieter
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
David White
2003-12-23 21:47:55 UTC
Permalink
Post by Victor Bazarov
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
Cy Edmunds
2003-12-23 22:25:31 UTC
Permalink
Post by Pieter
Hello,
//baseclass : Employee
class Employee{
string name;
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 {
int hours;
static double wage;
Manager(string);
void getHours(int); // <-----
virtual double Earnings();
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.
--
Cy
http://home.rochester.rr.com/cyhome/
Continue reading on narkive:
Loading...