Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I call the member function of the derived class

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
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 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.
Suggested Courses

721 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