Hiding of all Overloaded Methods with Same Name in Base Class in C++ Last Updated : 04 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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; } OutputDerived::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; } OutputBase::fun() called Comment More infoAdvertise with us Next Article Hiding of all Overloaded Methods with Same Name in Base Class in C++ kartik Follow Improve Article Tags : C Language C++ Practice Tags : CPP Similar Reads Functions that cannot be overloaded in C++ In C++, following function declarations cannot be overloaded. 1) Function declarations that differ only in the return type. For example, the following program fails in compilation. CPP #include<iostream> int foo() { return 10; } char foo() { return 'a'; } int main() { char x = foo(); getchar() 3 min read What are the Operators that Can be and Cannot be Overloaded in C++? There are various ways to overload Operators in C++ by implementing any of the following types of functions: 1) Member Function 2) Non-Member Function 3) Friend Function List of operators that can be overloaded are:Â + - Â * ? % ? Â & | Â ~ ! = Â < > += -= Â *= ?= %= ?= &= |= Â << 4 min read What happens when more restrictive access is given to a derived class method in C++? We have discussed a similar topic in Java here. Unlike Java, C++ allows to give more restrictive access to derived class methods. For example the following program compiles fine. C++ #include<iostream> using namespace std; class Base { public: virtual int fun(int i) { } }; class Derived: publi 2 min read Can main() be overloaded in C++? Predict the output of following C++ program. C highllight=12-5 #include <iostream> using namespace std; int main(int a) { cout << a << "\n"; return 0; } int main(char *a) { cout << a << endl; return 0; } int main(int a, int b) { cout << a << " 2 min read std::is_base_of template in C++ with Examples The std::is_base_of template of C++ STL is present in the <type_traits> header file. The std::is_base_of template of C++ STL is used to check whether class Base is the base class of Derived class or not. It return the boolean value true or false on the basis of above condition. Header File: #i 2 min read error: call of overloaded âfunction(x)â is ambiguous | Ambiguity in Function overloading in C++ Pre-requisite: Function Overloading in C++ Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. Two or more functions are 9 min read Advantages and Disadvantages of Function Overloading in C++ Function overloading is one of the important features of object-oriented programming. It allows users to have more than one function having the same name but different properties. Overloaded functions enable users to supply different semantics for a function, depending on the signature of functions. 3 min read How to Specify a Pointer to an Overloaded Function? In C++, function overloading allows multiple functions to have the same name but different parameter lists. While overloading is a powerful feature, it can lead to ambiguity when trying to assign a pointer to one of these overloaded functions. To resolve this ambiguity, specific methods can be used 4 min read Function Overloading and float in C++ Although polymorphism is a widely useful phenomena in C++ yet it can be quite complicated at times. For instance consider the following code snippet: CPP #include<iostream> using namespace std; void test(float s,float t) { cout << "Function with float called "; } void test(int 2 min read Why overriding both the global new operator and the class-specific operator is not ambiguous? The below section deals about overload resolution as it helps in the fundamentals of overloading and overriding. Predict the output: CPP #include <iostream> using namespace std; class Gfg { public: void printHello() { cout << "hello gfg-class specific" << endl; } }; void 5 min read Like