473,327 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Polymorphism

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
3 2061
"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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
3
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming ...
11
by: richard pickworth | last post by:
Can anyone explain polymorphism?(very simply). thanks richard
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.