Solved

How can I call the member function of the derived class

Posted on 2006-11-10
2
174 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 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 32
Visual c++ and text files 7 67
Getting IP address 8 79
C++ Language error 28 217
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

773 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