Solved

How can I call the member function of the derived class

Posted on 2006-11-10
2
176 Views
Last Modified: 2010-04-01
Hi Experts,

In the following code, xx->test(bb); calls the member function from the base class.  How can I change the code to get the derived class's member function.

I want the output to show that it is 'In derived' when I call the member function of X

Thanks,


#include <string>
#include <iostream>
using namespace std;

class A
{
public:
      A(){};
      void printPlace(){cout << "In super" << endl;};

};

      
class B: public A
{
public:
      void printPlace(){cout << "In derived" << endl;};

};


class X
{

public:

      void test(A *a){ a->printPlace();};

};

void main () {


      B *bb = new B();
      A *aa = new A();
      bb->printPlace();
      aa->printPlace();


                X *xx = new X();
      xx->test(bb);

}
0
Comment
Question by:ambuli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Author Comment

by:ambuli
ID: 17920131
Declaring

class A
{
public:
     A(){};
     virtual void printPlace(){cout << "In super" << endl;};

};


solved the problem.  But, Why?  

Thanks,
0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 17920145
Because by using the 'virtual' specfier, you tell the compileer that you expect the function to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question