473,771 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ 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 1888
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
4996
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 pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived object's public functions. Although, the base class pointer does call the appropriate virtual function...
7
1899
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
1626
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
4345
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 Methods?
5
3164
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 CommonPageBase - Contains myPage which inherits PageBase Each of these classes overrides OnInit and ties an event handler
7
5258
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 inheritance when the base classes are written to expect it, so "Always use super()" seems like bad advice. OTOH sometimes you need super() to get correct behaviour. ISTM "Only use super() when you know you need it" might be
1
431
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
2625
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
2236
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 modify come of the classes so they match our purpose better - mostly add a few methods etc. Example: Open source lib has classes Map and Layer defined. The relationship between them is one to many. We created our own versions (inherited) of Map...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6713
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.