8

I believe, a derived class can override only those functions which it inherited from the base class. Is my understanding correct.?

That is if base class has a public member function say, func, then the derived class can override the member function func.

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

Am i right?

Edit

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

/* 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;
}
Zain Rizvi
  • 23,586
  • 22
  • 91
  • 133
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127

6 Answers6

13

You can override functions regardless of access specifiers. That's also the heart of the non-virtual interface idiom. The only requirement is of course that they are virtual.

Xeo
  • 129,499
  • 52
  • 291
  • 397
12

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 can't. In C++, you can.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
5

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

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;
}

A way to see that 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();
}

Trying to compile this with g++ leads tot he 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 class Derived wouldn't inherit that function, the error message would be the same in both cases.

celtschk
  • 19,311
  • 3
  • 39
  • 64
  • In your example i can see that the function `do_operate_on` which is a private member in base class is overriden in the derived class. So, since private members are not inherited, the example above shows that functions can still be overriden although they are not inherited. Your answer confuses me a little because `Björn Pollex` and `FredOverflow` thinks differently from you. They say the private virtual functions can be overriden – nitin_cherian Nov 14 '11 at 14:43
  • +1 for the code sample which opened up mind to the use of private virtual member function. – nitin_cherian Nov 14 '11 at 15:03
  • @LinuxPenseur: I've now added example code demonstrating that the private function is indeed inherited. – celtschk Nov 14 '11 at 20:36
  • @FredOverflow : Please clarify. Put a bottomline for this "because FredOverflow said so" – nitin_cherian Nov 15 '11 at 05:27
0

No You can not over ride any function in base class . My reason for this notion is that if you define the function in derived class that has the same function signiture in base class , the base class function will become hidden for derived class .

For more information about this exciting 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

Infintyyy
  • 929
  • 1
  • 11
  • 23
-2

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

Andrii
  • 1,788
  • 11
  • 20
  • 1
    The type of inheritance has no effect on the ability to overload virtual functions. – Björn Pollex Nov 12 '11 at 10:44
  • @Björn Pollex, I think the type of inheritance affects the ability to override functions. The following code wont compile: http://ideone.com/h59qb – B Faley Nov 12 '11 at 11:22
  • @Meysam: This fails to compile because you are trying to implicitly convert a pointer-to-child to a pointer-to-base, which fails, because `base` is a private base-class (that's why the error is reported in `main`, instead of the definition of `child`). This error is not caused by the overriding. – Björn Pollex Nov 12 '11 at 11:25
-3

In order to override a function inherited from base class, it should be both virtual, and classified as public or protected.

B Faley
  • 17,120
  • 43
  • 133
  • 223
  • 2
    -1: This is not correct, you can override private virtual functions. – Björn Pollex Nov 12 '11 at 10:44
  • 1
    But this will: http://ideone.com/anUoc. In your example, you are trying to call a private function of `base`. Only `base` can do that. – Björn Pollex Nov 12 '11 at 10:55
  • Thank you for the code snippet :) I just don't understand what is the benefit of being private if the body of function can be altered in child classes so easily – B Faley Nov 12 '11 at 11:14
  • As @Xeo mentioned in his answer, this is used in the [non-virtual-interface-idiom](http://www.gotw.ca/publications/mill18.htm). – Björn Pollex Nov 12 '11 at 11:17