Inheritance and Overloading

Discussion in 'C++' started by Larry Lindsey, Sep 24, 2003.

  1. If I have classes Parent, Child, and Seat as follows

    class Parent{
    public:
    float getValue(){return 0;}
    ...
    }

    class Child: public Parent{
    public:
    float getValue(){return val;}
    ...
    private:
    float val;
    }

    class Seat{
    public:
    Parent* getPerson(){
    return thePerson;
    }
    void setPerson(Parent *inPerson){
    thePerson=inPerson;
    }
    ...
    private:
    Parent* thePerson;
    }

    If i do this:

    Child *c = new Child();
    Seat *s = new Seat();
    Child->setValue(12);
    s->setPerson(c);

    How would I make s->getPerson()->getValue() return 12, and not 0?

    --Larry
    Larry Lindsey, Sep 24, 2003
    #1
    1. Advertising

  2. Larry Lindsey

    jeffc Guest

    "Larry Lindsey" <> wrote in message
    news:bksrbi$et5$...
    > If I have classes Parent, Child, and Seat as follows
    >
    > class Parent{
    > public:
    > float getValue(){return 0;}
    > ...
    > }
    >
    > class Child: public Parent{
    > public:
    > float getValue(){return val;}
    > ...
    > private:
    > float val;
    > }
    >
    > class Seat{
    > public:
    > Parent* getPerson(){
    > return thePerson;
    > }
    > void setPerson(Parent *inPerson){
    > thePerson=inPerson;
    > }
    > ...
    > private:
    > Parent* thePerson;
    > }
    >
    > If i do this:
    >
    > Child *c = new Child();
    > Seat *s = new Seat();
    > Child->setValue(12);


    Did you forget to define this function?

    > s->setPerson(c);
    >
    > How would I make s->getPerson()->getValue() return 12, and not 0?


    Read up on virtual functions.
    jeffc, Sep 24, 2003
    #2
    1. Advertising

  3. "jeffc" <> wrote in message
    news:...
    >
    > "Larry Lindsey" <> wrote in message
    > news:bksrbi$et5$...
    > > If I have classes Parent, Child, and Seat as follows
    > >
    > > class Parent{
    > > public:
    > > float getValue(){return 0;}
    > > ...
    > > }
    > >
    > > class Child: public Parent{
    > > public:
    > > float getValue(){return val;}
    > > ...
    > > private:
    > > float val;
    > > }
    > >
    > > class Seat{
    > > public:
    > > Parent* getPerson(){
    > > return thePerson;
    > > }
    > > void setPerson(Parent *inPerson){
    > > thePerson=inPerson;
    > > }
    > > ...
    > > private:
    > > Parent* thePerson;
    > > }
    > >
    > > If i do this:
    > >
    > > Child *c = new Child();
    > > Seat *s = new Seat();
    > > Child->setValue(12);

    >
    > Did you forget to define this function?
    >
    > > s->setPerson(c);
    > >
    > > How would I make s->getPerson()->getValue() return 12, and not 0?

    >
    > Read up on virtual functions.
    >
    >


    Thanks
    Larry Lindsey, Sep 24, 2003
    #3
  4. Larry Lindsey

    red floyd Guest

    jeffc wrote:
    > "Larry Lindsey" <> wrote in message
    > news:bksrbi$et5$...
    >
    >>If I have classes Parent, Child, and Seat as follows
    >>
    >>class Parent{
    >>public:
    >> float getValue(){return 0;}
    >> ...
    >>}
    >>
    >>class Child: public Parent{
    >>public:
    >> float getValue(){return val;}
    >> ...
    >>private:
    >> float val;
    >>}
    >>
    >>class Seat{
    >>public:
    >> Parent* getPerson(){
    >> return thePerson;
    >> }
    >> void setPerson(Parent *inPerson){
    >> thePerson=inPerson;
    >> }
    >> ...
    >>private:
    >> Parent* thePerson;
    >>}
    >>
    >>If i do this:
    >>
    >>Child *c = new Child();
    >>Seat *s = new Seat();
    >>Child->setValue(12);

    >
    >
    > Did you forget to define this function?
    >
    >
    >>s->setPerson(c);
    >>
    >>How would I make s->getPerson()->getValue() return 12, and not 0?

    >
    >
    > Read up on virtual functions.
    >
    >

    In other words, because Parent::getValue() is non-virtual, and getPerson
    returns a Parent*, any reference to getValue() through getPerson() will access
    Parent::getValue().

    If you declare getValue() as virtual in Parent(), then it will behave in the
    polymorphic manner that you desire.
    red floyd, Sep 24, 2003
    #4
  5. "red floyd" <> wrote in message
    news:SEmcb.538$...
    > jeffc wrote:
    > > "Larry Lindsey" <> wrote in message
    > > news:bksrbi$et5$...
    > >
    > >>If I have classes Parent, Child, and Seat as follows
    > >>
    > >>class Parent{
    > >>public:
    > >> float getValue(){return 0;}
    > >> ...
    > >>}
    > >>
    > >>class Child: public Parent{
    > >>public:
    > >> float getValue(){return val;}
    > >> ...
    > >>private:
    > >> float val;
    > >>}
    > >>
    > >>class Seat{
    > >>public:
    > >> Parent* getPerson(){
    > >> return thePerson;
    > >> }
    > >> void setPerson(Parent *inPerson){
    > >> thePerson=inPerson;
    > >> }
    > >> ...
    > >>private:
    > >> Parent* thePerson;
    > >>}
    > >>
    > >>If i do this:
    > >>
    > >>Child *c = new Child();
    > >>Seat *s = new Seat();
    > >>Child->setValue(12);

    > >
    > >
    > > Did you forget to define this function?
    > >
    > >
    > >>s->setPerson(c);
    > >>
    > >>How would I make s->getPerson()->getValue() return 12, and not 0?

    > >
    > >
    > > Read up on virtual functions.
    > >
    > >

    > In other words, because Parent::getValue() is non-virtual, and getPerson
    > returns a Parent*, any reference to getValue() through getPerson() will

    access
    > Parent::getValue().
    >
    > If you declare getValue() as virtual in Parent(), then it will behave in

    the
    > polymorphic manner that you desire.
    >


    I've made everything virtual that needs to be virtual, but now I'm having a
    problem where private variables aren't keeping their values. Does anyone
    know whats going on?

    for instance, in the above example, after I call c->setValue(12), were I to
    call c->getValue(), it would return -8.4873e+008, which I'm pretty sure is
    2's complement 0x000000 for some number of 0's.

    Thanks for all of you help thus far, btw.
    Larry Lindsey, Sep 24, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cppsks
    Replies:
    0
    Views:
    803
    cppsks
    Oct 27, 2004
  2. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,394
    Fredrik Lundh
    Sep 30, 2005
  3. Fredrik Lundh
    Replies:
    0
    Views:
    438
    Fredrik Lundh
    Sep 30, 2005
  4. Steve Holden
    Replies:
    0
    Views:
    422
    Steve Holden
    Sep 30, 2005
  5. Iyer, Prasad C
    Replies:
    4
    Views:
    570
    John J. Lee
    Sep 30, 2005
Loading...

Share This Page