Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Hiding of all Overloaded Methods with Same Name in Base Class in C++

  • Difficulty Level : Medium
  • Last Updated : 04 Jan, 2022

In C++, function overloading is possible i.e., two or more functions from the same class can have the same name but different parameters. However, if a derived class redefines the base class member method then all the base class methods with the same name become hidden in the derived class. 

For example, the following program doesn’t compile. Here, Derived redefines Base’s method fun() and this makes fun(int i) hidden.

CPP




// CPP Program to demonstrate derived class redefines base
// class member method and generates compiler error
#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"; }
};
  
// Driver Code
int main()
{
    Derived d;
    d.fun(5); // Compiler Error
    return 0;
}

Output

prog.cpp: In function ‘int main()’:
prog.cpp:20:12: error: no matching function for call to ‘Derived::fun(int)’
    d.fun(5); // Compiler Error
           ^
prog.cpp:13:9: note: candidate: int Derived::fun()
    int fun() { cout << "Derived::fun() called"; }
        ^
prog.cpp:13:9: note:   candidate expects 0 arguments, 1 provided

Even if the signature of the derived class method is different, all the overloaded methods in the base class become hidden. For example, in the following program, Derived::fun(char ) makes both Base::fun() and Base::fun(int ) hidden.

CPP




// CPP Program to demonstrate derived class redefines base
// class member method
#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:
    // Makes Base::fun() and Base::fun(int )
    // hidden
    int fun(char c)
    {
        cout << "Derived::fun(char c) called";
    }
};
  
// Driver Code
int main()
{
    Derived d;
    d.fun('e'); // No Compiler Error
    return 0;
}
Output
Derived::fun(char c) called

Note: The above facts are true for both static and non static methods.

There is a way to mitigate this kind of issue. If we want to overload a function of a base class, it is possible to unhide it by using the ‘using’ keyword. This keyword brings a base class method ​or variable into the current class’s scope.

C++




// CPP Program to demonstrate derived class redefines base
// class member method using the 'using' keyword
#include <iostream>
using namespace std;
  
class Base {
public:
    int fun() { cout << "Base::fun() called"; }
};
  
class Derived : public Base {
public:
    using Base::fun;
  
    int fun(char c) // Makes Base::fun() and Base::fun(int )
                    // unhidden
    {
        cout << "Derived::fun(char c) called";
    }
};
  
// Driver Code
int main()
{
    Derived d;
    d.fun(); // Works fine now
    return 0;
}
Output
Base::fun() called

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!