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: Ambiguity in derived classesAn 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: Explanation:
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:
Example:Let us take a C++ program to illustrate the usage of "using" keyword 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. Next TopicHow does Duff's Device work in C++ |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India