Y
Y
yuharu2015-10-04 13:45:45
OOP
yuharu, 2015-10-04 13:45:45

How to call a child function with the same name as the parent (C++)?

I recently started learning OOP. But here's something that can't be sorted out. Here is an example program code. How to make it so that the function of the child is called in the main, and not the parent?

class tparent
{
...
public:
...
  void getdata() 
  {
          ...
  }
};

class tchild:public tparent
{
...
public:
  void getdata()
  {
         ...
  }
};

void main()
{
       tchild ob;
       ob.getdata();
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
werktone, 2015-10-04
@yuharu

In your code, the child function will be called.

N
Nikita Oleinik, 2015-10-04
@nikitosoleil

Prefix parent function with virtual

F
fuzz0, 2015-10-04
@fuzz0

en.cppreference.com/w/cpp/language/virtual

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question