Base class parameters called on Derived class: behaviour

Discussion in 'C++' started by Nagrik, Nov 28, 2007.

  1. Nagrik

    Nagrik Guest

    Dear Group,

    I am calling a function which takes a parameter of type base class and
    passing a
    concrete (compile time) derived class. The result is that it still
    calls the base class
    implementation and not the derived class.

    For parameter passed as an object, it does not matter whether the
    function is
    virtual or not, it still calls the base function. And if it is not
    virtual all calls are from
    base class.

    Can someone explain it.

    #include "stdafx.h"
    #include <iostream.h>

    class Pet {

    public:
    Pet() {}
    ~Pet() {}
    virtual void Say() {cout << "Growl" << endl;}
    void nonvirtSay() {cout << "Growl" << endl;}
    };

    void Make3Sounds( Pet* pptr, Pet& pref, Pet pval);

    class Cat : public Pet {

    public:
    Cat() {}
    ~Cat() {}
    virtual void Say() {cout << "Meaw" << endl;}
    void nonvirtSay() {cout << "Meaw" << endl;}
    };

    int main(int argc, char* argv[])
    {

    Cat c;
    Make3Sounds(&c, c, c);
    return 0;
    }


    void Make3Sounds( Pet* pptr, Pet& pref, Pet pval) {
    pptr->Say();
    pref.Say();
    pval.Say();

    pptr->nonvirtSay();
    pref.nonvirtSay();
    pval.nonvirtSay();

    }


    Result:
    Meaw
    Meaw
    Growl
    Growl
    Growl
    Growl

    Thanks

    arun
    Nagrik, Nov 28, 2007
    #1
    1. Advertising

  2. Nagrik wrote:
    > I am calling a function which takes a parameter of type base class and
    > passing a
    > concrete (compile time) derived class. The result is that it still
    > calls the base class
    > implementation and not the derived class.
    >
    > For parameter passed as an object, it does not matter whether the
    > function is
    > virtual or not, it still calls the base function. And if it is not
    > virtual all calls are from
    > base class.
    >
    > Can someone explain it.


    Read about slicing and static typing.

    >
    > #include "stdafx.h"
    > #include <iostream.h>


    C++ has no such header for about 10 years.

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 28, 2007
    #2
    1. Advertising

  3. Nagrik a écrit :
    > Dear Group,
    >
    > I am calling a function which takes a parameter of type base class and
    > passing a
    > concrete (compile time) derived class. The result is that it still
    > calls the base class
    > implementation and not the derived class.
    >
    > For parameter passed as an object, it does not matter whether the
    > function is
    > virtual or not, it still calls the base function. And if it is not
    > virtual all calls are from
    > base class.


    For the third parameter of Make3Sounds, it performs a copy of Cat into
    Pet parameter so it is no longer a Cat but a simple pet. This is called
    slicing.



    >
    > Can someone explain it.
    >
    > #include "stdafx.h"
    > #include <iostream.h>
    >
    > class Pet {[snip]
    > };
    >
    > void Make3Sounds( Pet* pptr, Pet& pref, Pet pval);
    >
    > class Cat : public Pet {[snip]
    > };[snip}
    Michael DOUBEZ, Nov 28, 2007
    #3
  4. Nagrik:

    > void Make3Sounds( Pet* pptr, Pet& pref, Pet pval) {
    > pptr->Say();
    > pref.Say();




    These two produce "Meaw" for the reasons you know, i.e. "Say" is a
    virtual function.



    > pval.Say();




    This produces "Growl" because when you pass by value, a new object gets
    created, and the new object is of type "Pet" rather than "Cat". It's as
    if you did:

    Cat cat;

    Pet pet(cat);


    I.e. you're creating a new Pet out of a Cat.



    > pptr->nonvirtSay();
    > pref.nonvirtSay();
    > pval.nonvirtSay();




    These all say "Growl" because:

    a) You're dealing with a Pet
    and
    b) The function "nonvirtSay" is non-virtual, i.e. not polymorphic.

    , therefore Pet's version of "nonvirtSay" is invoked every time.

    --
    Tomás Ó hÉilidhe
    Tomás Ó hÉilidhe, Nov 28, 2007
    #4
    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. z. f.
    Replies:
    0
    Views:
    549
    z. f.
    Oct 19, 2004
  2. Replies:
    4
    Views:
    389
    Alf P. Steinbach
    May 23, 2007
  3. Replies:
    1
    Views:
    384
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    372
    Victor Bazarov
    May 23, 2007
  5. Replies:
    2
    Views:
    692
Loading...

Share This Page