Hiding of all overloaded methods in base class in C++


In C++, we can use the function overloading techniques. But if some base class has one method in overloaded form (different function signature with the same name), and the derived class redefines one of the function which is present inside the base, then all of the overloaded version of that function will be hidden from the derived class.

Let us see one example to get the clear idea.

Example

#include <iostream>
using namespace std;
class MyBaseClass {
   public:
      void my_function() {
         cout << "This is my_function. This is taking no arguments" << endl;
      }
      void my_function(int x) {
         cout << "This is my_function. This is taking one argument x" << endl;
      }
};
class MyDerivedClass : public MyBaseClass {
   public:
      void my_function() {
         cout << "This is my_function. From derived class, This is taking no arguments" << endl;
      }
};
main() {
   MyDerivedClass ob;
   ob.my_function(10);
}

Output

[Error] no matching function for call to 'MyDerivedClass::my_function(int)'
[Note] candidate is:
[Note] void MyDerivedClass::my_function()
[Note] candidate expects 0 arguments, 1 provided
raja
Published on 08-May-2019 13:13:10
Advertisements