Hiding of all overloaded methods with same name in base class in C++
A class in C++ can have multiple methods with the same name but distinct parameter lists due to method overloading. Nevertheless, the overloaded methods of the base class may be hidden if you derive a class from it and overload a method with the same name in the derived class. It is referred to as "method hiding", and, if left unchecked, it can result in unexpected behavior. When a derived class declares a method with the same name as a base class overloaded method, it is known as method hiding. The overloaded methods in the base class are hidden within the scope of the derived class. As a result, the derived class's method will be called if you call it using an object of the derived class, even if the base class has a method that is identical.
Methods Used Are:
- Method Overloading:
A C++ feature called "method overloading" enables a class to have multiple methods with the same name but distinct parameter lists. In the example, there are two overloaded versions of the Base class's display method: one that takes an int parameter and the other that takes a double parameter.
- Inheritance:
The Derived class is generated from the Base class by using the public inheritance specifier. A "is-a" relationship is exists between the base and derived classes through inheritance.
- Method Hiding:
When a derived class declares a method that is identical to an overloaded method in the base class, the overloaded methods in the base class become hidden inside the derived class's scope. This phenomenon is known as method hiding.
- Declaration:
A C++ feature called the declaration allows you to include specific names from a base class in the scope of a derived class. It uses the Base::display() in the Derived class to avoid method hiding and make the overloaded methods of the base class visible in the derived class.
Example:
Let's take an example to illustrate how to hide all overloaded methods with same name in base class in C++.
#include <iostream>
class Base {
public:
void display(int value) {
std::cout << "Base class display with int: " << value << std::endl;
}
void display(double value) {
std::cout << "Base class display with double: " << value << std::endl;
}
};
class Derived : public Base {
public:
// Hides the base class display(int) method
void display(int value) {
std::cout << "Derived class display with int: " << value << std::endl;
}
};
int main() {
Derived derivedObj;
// Calls the display(int) method in the Derived class
derivedObj.display(142);
// Calls the display(double) method in the Base class
derivedObj.display(3.14);
return 0;
}
Output:
Derived class display with int: 142
Derived class display with int: 3
Example 2:
Let's take another example to illustrate how to hide all overloaded methods with same name in base class in C++.
#include <iostream>
using namespace std;
class Base {
public:
void display() { cout << "Base::display() called" << endl; }
void display(int i) { cout << "Base::display(int i) called with value: " << i << endl; }
};
class Derived : public Base {
public:
// Change the parameters and contents in the overridden method
void display(char ch) { cout << "Derived::display(char ch) called with character: " << ch << endl; }
};
// Driver Code
int main() {
Derived d;
// Calls the Base class's display(int i) method
d.display(42);
// Calls the Derived class's display(char ch) method
d.display('A');
return 0;
}
Output:
Derived::display(char ch) called with character: *
Derived::display(char ch) called with character: A
Example 3:
Let's take another example to illustrate how to hide all overloaded methods with same name in base class in C++.
#include <iostream>
using namespace std;
class Base {
public:
void display() { cout << "Base::display() called" << endl; }
};
class Derived : public Base {
public:
using Base::display;
// Change the parameters and contents in the added method
void display(char ch) {
cout << "Derived::display(char ch) called with character: " << ch << endl;
}
};
// Driver Code
int main() {
Derived d;
// Calls the Base class's display() method
d.display();
// Calls the Derived class's display(char ch) method
d.display('B');
return 0;
}
Output:
Base::display() called
Derived::display(char ch) called with character: B
Conclusion:
In conclusion, the overloaded methods from the base class are hidden within the scope of the derived class when a derived class redefines or overrides a method from the base class. It is the idea behind hiding overloaded methods with the same name in a base class in C++. The term "method hiding" refers to this phenomenon, which can cause unexpected behaviour and make code maintenance challenging. The using declaration in the derived class, which specifically brings the overloaded methods of the base class back into visibility, can be used by developers to address method hiding. It reduces ambiguity in method invocation and gives developers more control over the version of the method that is used in various scenarios. It is essential to comprehend and manage method hiding when designing and maintaining object-oriented C++ code to ensure that the intended behaviour of methods is preserved across class hierarchies and to promote code clarity and predictability. In this situation, developers can make well-informed decisions about method visibility and invocation in derived classes thanks to the useful tool that is the 'using' declaration.