Я считаю, что производный класс
может переопределить
только те функции, которые он унаследовал от базовый класс
. Правильно ли я понимаю.?
То есть, если базовый класс имеет общедоступную функцию-член, скажем, func
, то производный класс может переопределить
функцию-член func
.
Но если базовый класс имеет частную функцию-член, скажем foo
, то производный класс не может переопределить функцию-член foo
.
Я прав?
Я придумал образец кода после изучения ответов, данных членами SO. Я упоминаю пункты, которые я изучал, в виде комментариев в коде. Надеюсь, я прав. Спасибо
/* Points to ponder:
1. Irrespective of the access specifier, the member functions can be override in base class.
But we cannot directly access the overriden function. It has to be invoked using a public
member function of base class.
2. A base class pointer holding the derived class obj's address can access only those members which
the derived class inherited from the base class. */
#include <iostream>
using namespace std;
class base
{
private:
virtual void do_op()
{
cout << "This is do_op() in base which is pvt\n";
}
public:
void op()
{
do_op();
}
};
class derived: public base
{
public:
void do_op()
{
cout << "This is do_op() in derived class\n";
}
};
int main()
{
base *bptr;
derived d;
bptr = &d;
bptr->op(); /* Invoking the overriden do_op() of derived class through the public
function op() of base class */
//bptr->do_op(); /* Error. bptr trying to access a member function which derived class
did not inherit from base class */
return 0;
}