473,466 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Base class parameters called on Derived class: behaviour

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
Nov 28 '07 #1
3 1878
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
Nov 28 '07 #2
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}
Nov 28 '07 #3
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
Nov 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
7
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
8
by: Andreas Lagemann | last post by:
Hi, after browsing FAQ and archive for a while I decided that the following is a legal question. Consider this: Class Base { public: Base() {}
4
by: Akhil | last post by:
Hi All, Can u please explain this. Base Obj = new Derived(); Can Obj access methods both of Base and Derived or what will be the behaviour? What will be the behaviour for Overridden...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
7
by: Kent Johnson | last post by:
Are there any best practice guidelines for when to use super(Class, self).__init__() vs Base.__init__(self) to call a base class __init__()? The super() method only works correctly in multiple...
1
by: Fedor Semenov | last post by:
How can I call a base class' constructor from a derived class. Suppose I have: class Button // Base class { public: Button(void) { // Register classes, create the button, etc. } };
5
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.