To override a function in a derived class, is it required to inherit from the base class?

I suppose a derived class can override only use functions that it inherits from the base class . Do I understand correctly.?

That is, if the base class has an open member function, say func , then the derived class can override use the func member function.

But if the base class has a private member function, say, foo , then the derived class cannot override the member function foo .

I'm right?

Edit

I came up with a sample code after examining the answers given by the SO members. I mention those points that I studied as comments in the code. I hope I'm right. Thanks

 /* 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 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; } 
+4
source share
6 answers

You can redefine functions regardless of access specifiers. It is also the heart of a non-virtual idiom interface . The only requirement, of course, is virtual .

+8
source

But if the base class has a private member function, say, foo , then the derived class cannot override the member function foo .

In Java you cannot. In C ++ you can.

+7
source

You are right that in order to redefine a function in a derived class, it must inherit it from the base class (in addition, the function of the base class must be virtual). However, you are mistaken in your assumption that virtual functions are not inherited. For example, the following works well (and is actually a well-known idiom for checking for a precondition / postcondition):

 class Base { public: void operate_on(some thing); private: virtual void do_operate_on(some thing) = 0; }; void Base::operate_on(some thing) { // check preconditions do_operate_on(thing); // check postconditions } class Derived: public Base { // this overrides Base::do_operate_on void do_operate_on(some thing); }; void Derived::do_operate_on(some thing) { // do something } int main() { some thing; Base* p = new Derived; // this calls Base::operate_on, which in turn calls the overridden // Derived::do_operate_on, not Base::do_operate_on (which doesn't have an // implementation anyway) p->operate_on(thing); delete p; } 

The way to see if private methods are really inherited is to look at the error messages generated by the following code:

 class Base { private: void private_method_of_B(); }; class Derived: public Base { }; int main() { Derived d; d.private_method_of_B(); d.method_that_does_not_exist(); } 

Attempting to compile this with g ++ results in the following error messages:

 privatemethodinheritance.cc: In function 'int main()': privatemethodinheritance.cc:4: error: 'void Base::private_method_of_B()' is private privatemethodinheritance.cc:15: error: within this context privatemethodinheritance.cc:16: error: 'class Derived' has no member named 'method_that_does_not_exist' 

If the Derived class does not inherit this function, the error message will be the same in both cases.

+3
source

No. You cannot overcome any function in the base class. My reason for this is that if you define a function in a derived class that has the same function sign in the base class, the base class function will become hidden for the derived class.

For more information about this interesting issue, just visit: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage% 2Fref% 2Foverload_member_fn_base_derived. htm

0
source

It also depends on the type of inheritance.
class Derived : [public | protected | private] Base { };

-2
source

To override a function inherited from the base class, it must be virtual and classified as public or protected .

-3
source

Source: https://habr.com/ru/post/1380893/


All Articles