Solved

How can I call the member function of the derived class

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now