473,657 Members | 2,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

doubt in virtual funtion

52 New Member
Hi All,
I am new to c++, please find my program below and my doubt as mentioned below.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class base
  5. {
  6.     public:
  7.         virtual void Show(void)
  8.         {
  9.             cout << "base:Show" << endl;
  10.             return ;
  11.         }
  12.         void Test(base obj)
  13.         {
  14.             obj.Show();
  15.         }
  16. };
  17. class derived:public base
  18. {
  19.     public:
  20.         void Show(void)
  21.         {
  22.             cout << "derived:Show" << endl;
  23.             return ;
  24.         }
  25. };
  26. int main(int argc, char **argv)
  27. {
  28.     derived d1;
  29.     derived d2;
  30.     d1.Test(d2);
  31.     return 0;
  32. }
  33.  
  34.  

When I call this d1.Test(d2) , its always refers baseclass show funtion( ).Why its refering always base class member funtion. please let me know, what is referring in the backend!
Dec 21 '09 #1
1 1837
Banfa
9,065 Recognized Expert Moderator Expert
The parameter of test has type base. That is it passes an object. What actually happens is the base copy constructor is called to construct a new base object from the derived object d2 that is passed. Inside test since obj has type base it calls base::Show.

I suspect you intended to pass a reference to base into test, base& then when you call test it will pass a base reference to d2 but when you call Show it will call derived::Show because the object is actually a derived object even though it is a base reference.
Dec 21 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1580
by: garyolsen | last post by:
Case 1: ============== class MyClass { public: virtual void MyVirt(); }; class MyChildClass : public MyClass
9
1903
by: jlopes | last post by:
There seems to bet no diff between a vitual method and an inheirited method. class A_Base { public: virtual void filter(){ /* some code */ } }; class D_of_A_Base : public A_Base {
11
4351
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining it inline. Like this.
10
1335
by: Serg | last post by:
Every time i am trying to step into virtual funtion debugger tells me "there is no source code available" and disassemble window appears. If function is non virtual debugger succefully steps into it and shows source code. Is there any compiler key that can fix it? Please help. Thanks in adv.
8
2888
by: rakoo | last post by:
I want to question about this virtual keyword , what is neccessty of it .. when base class ponter or simply object assingned to derived class object ,we never want that base class funtion by base class object or pointer which pointing to derived class object Are we ? then why stroupstrop has given this keyword
1
286
by: Rahul K | last post by:
Hi all I tried running the following code: #include<iostream.h> class Base { public: virtual void func()
3
11832
by: Jimmy | last post by:
I was browsing the C++ FAQ Lite the other day when I came accross question #23.4 (http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.4). The question was "When should someone use private virtuals?", and the author answered with "almost never, but there are reasons". So, I am asking, what are the reasons. It seems illogical to have one; the point of a virtual function is to be overriden in a derived class, and the...
3
1690
by: shuisheng | last post by:
Dear All, If I define a virtual function to be inline, is it really inline? Or it is inline in some cases, and not in other cases. Would you please help me to look at the following case. struct A { int i; A() {i = 0;}
1
1996
by: imvc | last post by:
as we can access virtual function using the hidden vptr but we can also access virtual private member function using _vptr so its a loophole in the language isn't it ?
0
8829
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
8734
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
8508
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
8608
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7341
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5633
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
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2733
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
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.