Hiding Of All Overloading Methods With The Same Name In The Base Class In C++ - C++ Programming Tutorial
C++ Course / Polymorphism / Hiding Of All Overloading Methods With The Same Name In The Base Class In C++

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

BLUF: Mastering Hiding Of All Overloading Methods With The Same Name In The Base Class In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Hiding Of All Overloading Methods With The Same Name In The Base Class In C++

C++ is renowned for its efficiency. Learn how Hiding Of All Overloading Methods With The Same Name In The Base Class In C++ enables low-level control and high-performance computing in the tutorial below.

In C++, when a base class contains multiple overloaded methods sharing the same name, developers have the option to conceal them in the derived class through the "using" declaration. This practice is commonly referred to as method hiding. The forthcoming discussion will elaborate on the technique of concealing all overloaded methods having identical names in the base class in C++.

Understanding method overloading in C++

In C++, the concept of overloading enables a class to contain multiple functions sharing the same name. However, each function distinguishes itself through varying parameter types, argument counts, and return types. This practice of overloading enhances code readability by facilitating the creation of functions that can execute similar tasks with diverse argument types or 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's consider a C++ program to demonstrate 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

A problem might occur when a class inherits from a base class containing an overloaded method. If the derived class also includes a method with the identical name, it can result in ambiguity, potentially causing the compiler to produce an error.

Example:

Let's consider a C++ code example to demonstrate ambiguity in a subclass:

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 subclass extends a superclass that contains multiple overloaded methods with identical names, method hiding can be utilized to selectively conceal those overloaded methods within the subclass. This is accomplished through the "using" declaration, enabling the subclass to explicitly define which superclass methods with the same name should be reachable.

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's consider a C++ program to demonstrate the application of the "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 software, there exist two categories: Employee and Manager. Within the Employee category, there are multiple versions of functions that determine bonus amounts using different criteria, such as years of service or performance evaluations. The Manager category, which is a subclass of Employee, presents a fresh function that computes a comprehensive bonus by taking into account both years of service and performance evaluations. By employing the statement using Employee::calculateBonus; inside the Manager class, the overloaded functions in the parent class are concealed.

An object of the Manager class, assigned the name manager, is instantiated within the main function. Following this, the software triggers the calculateBonus function in the Manager class using arguments 5 and 4.5, showcasing the bonus total for 5 years of service and a performance rating of 4.5. Afterwards, the application accesses the methods of the base class via the derived class: invoking calculateBonus(7) for 7 years of service and calculateBonus(4.2) for a performance rating of 4.2. Each function call exhibits the computed bonus along with an appropriate message.

Conclusion:

In summary, method hiding in C++ enables developers to conceal overloaded methods from a base class in a derived class by utilizing the "using" declaration. This feature improves code transparency, prevents confusion, and grants management of the derived class interface. Method overloading, a connected idea, permits programmers to establish functions with identical names but distinct parameters, enhancing code comprehension. The instances showcase these principles, displaying their tangible utilization in practical situations.

Input Required

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

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience