473,387 Members | 1,398 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,387 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 2066
"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.