Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I call the member function of the derived class

Posted on 2006-11-10
2
Medium Priority
?
179 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
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 1200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

782 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