Note that there are some explanatory texts on larger screens.

plurals
  1. POCan you override private functions defined in a base class?
    primarykey
    data
    text
    <p>I believe, a <code>derived class</code> can <code>override</code> only those functions which it inherited from the <code>base class</code>. Is my understanding correct.?</p> <p>That is if base class has a public member function say, <code>func</code>, then the derived class can <code>override</code> the member function <code>func</code>.</p> <p>But if the base class has a private member function say, <code>foo</code>, then the derived class cannot override the member function <code>foo</code>.</p> <p>Am i right?</p> <h2>Edit</h2> <p>I have come up with a code sample after studying the answers given by SO members. I am mentioning the points which i studied as comments in the code. Hope i am right. Thanks</p> <pre><code>/* 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 &lt;iostream&gt; using namespace std; class base { private: virtual void do_op() { cout &lt;&lt; "This is do_op() in base which is pvt\n"; } public: void op() { do_op(); } }; class derived: public base { public: void do_op() { cout &lt;&lt; "This is do_op() in derived class\n"; } }; int main() { base *bptr; derived d; bptr = &amp;d; bptr-&gt;op(); /* Invoking the overriden do_op() of derived class through the public function op() of base class */ //bptr-&gt;do_op(); /* Error. bptr trying to access a member function which derived class did not inherit from base class */ return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload