473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

doubt in virtual funtion

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 1806
Banfa
9,065 Expert Mod 8TB
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
by: garyolsen | last post by:
Case 1: ============== class MyClass { public: virtual void MyVirt(); }; class MyChildClass : public MyClass
9
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
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...
10
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...
8
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...
1
by: Rahul K | last post by:
Hi all I tried running the following code: #include<iostream.h> class Base { public: virtual void func()
3
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...
3
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. ...
1
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.