Hiding of all overloaded methods with same name in base class
In C++, if a derived class redefines base class member method then all the base class methods with same name become hidden in derived class.
For example, the following program doesn’t compile. In the following program, Derived redefines Base’s method fun() and this makes fun(int i) hidden.
#include<iostream> using namespace std; class Base { public: int fun() { cout<<"Base::fun() called"; } int fun(int i) { cout<<"Base::fun(int i) called"; } }; class Derived: public Base { public: int fun() { cout<<"Derived::fun() called"; } }; int main() { Derived d; d.fun(5); // Compiler Error return 0; } |
chevron_right
filter_none
Even if the signature of the derived class method is different, all the overloaded methods in base class become hidden. For example, in the following program, Derived::fun(char ) makes both Base::fun() and Base::fun(int ) hidden.
#include<iostream> using namespace std; class Base { public: int fun() { cout<<"Base::fun() called"; } int fun(int i) { cout<<"Base::fun(int i) called"; } }; class Derived: public Base { public: int fun(char c) // Makes Base::fun() and Base::fun(int ) hidden { cout<<"Derived::fun(char c) called"; } }; int main() { Derived d; d.fun(); // Compiler Error return 0; } |
chevron_right
filter_none
Note that the above facts are true for both static and nonstatic methods.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Virtual base class in C++
- Publicly inherit a base class but making some of public method as private
- Functions that cannot be overloaded in C++
- Can main() be overloaded in C++?
- What are the operators that can be and cannot be overloaded in C++?
- Encode an ASCII string into Base-64 Format
- Catching base and derived classes as exceptions
- How to convert a class to another class type in C++?
- Different methods to reverse a string in C/C++
- Default Methods in C++ with Examples
- 5 Different methods to find length of a string in C++
- Methods to concatenate string in C/C++ with Examples
- C++: Methods of code shortening in competitive programming
- Taking String input with space in C (3 Different Methods)
- Calling virtual methods in constructor/destructor in C++

