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:

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

Output:

Hiding of all overloading methods with the same name in the base class in C++

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:

Output:

Hiding of all overloading methods with the same name in the base class in C++

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

Output:

Hiding of all overloading methods with the same name in the base class in C++

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.






Latest Courses