Polymorphism

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pieter

    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,Employe e> 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

    #2
    Re: Polymorphism

    "Pieter" <pieterdm@hotma il.com> wrote...[color=blue]
    > //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,Employe e> 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[/color]
    stored[color=blue]
    > as an Employee???? I hope not..).[/color]

    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_.
    [color=blue]
    >
    > 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??[/color]


    You need to store pointers to Employee:

    map<int,Employe e*>

    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 "polymorphi sm STL container" for more
    information (no double quotes, of course).

    Victor


    Comment

    • David White

      #3
      Re: Polymorphism

      "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
      news:5pZFb.4354 14$Dw6.1334408@ attbi_s02...[color=blue]
      > You need to store pointers to Employee:
      >
      > map<int,Employe e*>
      >
      > but make sure you create your Managers or Salesmen _dynamically_ and
      > dispose of them properly when you don't need them any longer.[/color]

      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



      Comment

      • Cy Edmunds

        #4
        Re: Polymorphism

        "Pieter" <pieterdm@hotma il.com> wrote in message
        news:b4ZFb.9297 2$0l5.4170821@p hobos.telenet-ops.be...[color=blue]
        > 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,Employe e> 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[/color]
        stored[color=blue]
        > 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"; )
        >
        >
        > */[/color]

        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



        Comment

        Working...