Understanding Function Hiding in C++ Object Oriented Programming
Yamil Garcia
Tech enthusiast, embedded systems engineer, and passionate educator! I specialize in Embedded C, Python, and C++, focusing on microcontrollers, firmware development, and hardware-software integration.
In C++ Object Oriented Programming (OOP), function hiding is a concept that often confuses beginners. It refers to the scenario where a derived class has a function with the same name as a function in its base class, causing the base class function to be inaccessible with the derived class object. This article aims to demystify function hiding with clear explanations and code examples.
What is Function Hiding?
Function hiding occurs in C++ when a derived class declares a function with the same name as a function in its base class, but does not override it. This is because C++ does not support polymorphism for functions that differ only by their parameters. When the derived class introduces a function with the same name, the base class version of the function is hidden and not accessible through the derived class object, unless specifically told to do so using the scope resolution operator.
Code Example 1: Demonstrating Function Hiding
n this example, the Derived class hides the show() function of the Base class because it introduces its own version of show() that takes an integer parameter. Attempting to call d.show() without parameters will result in a compile-time error because the compiler only sees the show(int) version in the Derived class.
How to Avoid Function Hiding
To avoid unintentional function hiding, C++ provides a way to explicitly indicate that a derived class function overrides a base class function using the using directive or the override keyword for virtual functions.
Code Example 2: Preventing Function Hiding with using
领英推荐
By adding using Base::show; inside the Derived class, we make the show() function from the Base class visible to the Derived class objects. This resolves the function hiding issue, allowing both show() and show(int) to be called on Derived objects.
Here's an example where a derived class overrides a virtual function from its base class, demonstrating the correct use of the override keyword:
Code Example 3: Using override with Virtual Functions
In this example:
This example illustrates how the override keyword ensures that the derived class function is indeed overriding a base class virtual function, providing a safer and clearer way to manage polymorphism in C++.
Conclusion
Function hiding in C++ is an important concept that can lead to confusing bugs if not understood properly. By recognizing when and why function hiding occurs, and knowing how to use the using directive to expose hidden base class functions, programmers can write clearer and more correct C++ code. Remember that explicit is better than implicit, especially in complex object-oriented hierarchies, to ensure your intentions are clear and your code behaves as expected.
Software Engineer | Data | AI | Automation | CS | IT | Research
1 年yes, the concept of hiding a code means the client/manger doesn't need to see that piece of code becaue sometimes code may change in the future according to the use case. by the way well writen