Hiding Of All Overloading Methods With The Same Name In The Base Class In C++

In C++, if multiple overloaded methods exist in a base class with the same name, programmers can hide them in the derived class by using the "using" declaration. It is known as method hiding. In this article, we will discuss how to hide all overloading methods with the same name in the base class in C++.

Understanding method overloading in C++

In C++, overloading makes a class have multiple functions with the same name, but parameter types will be different, the number of arguments is different, and the return type will be different. This concept will make the code more readable. It allows the developers to create functions that perform similar tasks to different types or arguments combinations.

Syntax of the method overloading:

It has the following syntax:

Example

class Base {
public:
void display(int x) {
 // Implementation for int version
 
}
void display(double y) {
 // Implementation for double version
 
}
};

Example:

Let us take a C++ program to illustrate the method overloading :

Example

#include <iostream>
using namespace std;
class Calculator
{
public:
 int add(int a, int b)
 {
 cout << "Adding two integers: ";
 return a + b;
 }
 double add(double a, double b)
 {
 cout << "Adding two doubles: ";
 return a + b;
 }
};
int main()
{
 Calculator myCalculator;
 int resultInt = myCalculator.add(5, 7);
 cout << resultInt << endl;
 double resultDouble = myCalculator.add(3.5, 2.7);
 cout << resultDouble << endl;
 return 0;
}

Output:

Ambiguity in derived classes

An issue may arise when a class inherits from a base class with the overloaded method . If derived class also introduces a method with the same name, it leads to ambiguity, and the compiler may generate an error.

Example:

Let us take a C++ program to show the ambiguity in a derived class:

Example

#include <iostream>
using namespace std;
class Animal
{
public:
 void makeSound()
 {
 cout << "Generic animal sound" << endl;
 }

 void makeSound(int loudness)
 {
 cout << "Generic animal sound with loudness: " << loudness << endl;
 }
};
class Dog : public Animal
{
public:
 // Introducing a new method in the derived class
 void makeSound(bool isBarking)
 {
 if (isBarking)
 {
 cout << "Dog is barking!" << endl;
 }
 else
 {
 Animal::makeSound();
 }
 }
};
int main()
{
 Dog myDog;
 myDog.makeSound(); // this line would lead to ambiguity
 myDog.makeSound(true);
 myDog.makeSound(5); // Calls the base class makeSound(int) method
 return 0;
}

Output:

Explanation:

  • In this example, the Animal class has two overloaded makeSound methods: one without parameters and another with an int parameter for loudness.
  • The Dog class inherits from Animal and introduces a new makeSound method that includes an additional parameter for specifying whether the dog is barking.
  • Suppose you uncomment the line // myDog.makeSound; , you will encounter a compilation error due to ambiguity, as the compiler won't know which makeSound method to call.
  • Hiding of all Overloading methods:

When a derived class inherits from a base class with multiple overloaded classes with the same name, method hiding can be employed to selectively hide those overloaded methods in the derived class. It is achieved using the "using" declaration, which allows the derived class to explicitly specify which base class methods with the same name should be accessible.

Advantages of hiding overloaded methods with the same name in the base class:

There are several advantages of hiding overloaded methods with the same name in the base class. Some of them are as follows:

  • Method hiding improves code clarity by explicitly defining which methods from the base class are accessible in the derived class. It will remove the ambiguity.
  • Hiding overloaded methods helps avoid ambiguity in cases where the derived class introduces methods with the same names, preventing confusion for both developers and the compiler.
  • It provides developers with control over the interface of the derived class. By hiding specific methods, developers can tailor the interface to better suit the requirements of the derived class.
  • Example:

Let us take a C++ program to illustrate the usage of "using" keyword

Example

#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
 void calculateBonus(int yearsOfService) {
 double bonus = yearsOfService * 100;
 cout << "Bonus for " << yearsOfService << " years of service: " << bonus << endl;
 }
 void calculateBonus(double performanceRating) {
 double bonus = performanceRating * 500;
 cout << "Bonus for performance rating " << performanceRating << ": " << bonus << endl;
 }
};
class Manager : public Employee {
public:
 // Hiding the overloaded methods from the base class
 using Employee::calculateBonus;
 // New method in the derived class
 void calculateBonus(int yearsOfService, double performanceRating) {
 double bonus = yearsOfService * 100 + performanceRating * 500;
 cout << "Manager's total bonus for " << yearsOfService << " years of service and performance rating " << performanceRating << ": " << bonus << endl;
 }
};

int main() {
 Manager manager;
 // Calls the derived class method
 manager.calculateBonus(5, 4.5);
 manager.calculateBonus(7); // Calls the base class calculateBonus(int) method
 manager.calculateBonus(4.2); // Calls the base class calculateBonus(double) method
 return 0;
}

Output:

Explanation:

In this program, there are two classes: Employee and Manager . The Employee class has overloaded methods for calculating bonuses based on either year of service or performance ratings. The Manager class inherits from Employee and introduces a new method that calculates a total bonus considering both years of service and performance rating. The using Employee::calculateBonus; statement in the Manager class hides the overloaded methods from the base class.

An instance of the Manager class, named manager, is created in the main function. After that, the program calls the new calculateBonus method in the Manager class with values 5 and 4.5, printing the total bonus for 5 years of service and a performance rating of 4.5. Subsequently, the program invokes the base class methods through the derived class: calculateBonus(7) for 7 years of service and calculateBonus(4.2) for a performance rating of 4.2. Each method call displays the calculated bonus with a respective message.

Conclusion:

In conclusion, method hiding in C++ allows programmers to selectively hide overloaded methods from a base class in a derived class using the "using" declaration. It enhances code clarity, avoids ambiguity, and provides control over the derived class interface. Method overloading, a related concept, allows developers to create functions with the same name but different parameters, improving code readability. The examples illustrate these concepts, demonstrating their practical application in real-world scenarios.

Input Required

This code uses input(). Please provide values below: