By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

Polymorphism

P: n/a
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";)
*/

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Pieter" <pi******@hotmail.com> wrote...
//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
Jul 22 '05 #2

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:5pZFb.435414$Dw6.1334408@attbi_s02...
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

Jul 22 '05 #3

P: n/a
"Pieter" <pi******@hotmail.com> wrote in message
news:b4*********************@phobos.telenet-ops.be...
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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.