Base class parameters called on Derived class: behaviour

N

Nagrik

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
 
V

Victor Bazarov

Nagrik said:
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
 
M

Michael DOUBEZ

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}
 
T

Tomás Ó hÉilidhe

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top