Understanding Function Hiding in C++ Object Oriented Programming
https://commons.wikimedia.org/wiki/File:C%2B%2B_logo.png

Understanding Function Hiding in C++ Object Oriented Programming

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

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

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

Using override with Virtual Functions

In this example:

  • The Base class has a virtual function show() that prints a message indicating it belongs to the Base class.
  • The Derived class overrides this function with its own implementation using the override keyword, ensuring that any calls to show() on a Base pointer or reference that points to a Derived object will invoke the Derived class's show() method.
  • Additionally, the Derived class introduces a new function, show(int), which does not hide the Base::show() due to the use of virtual functions and the override keyword. This new function is specific to the Derived class and demonstrates how function overloading works alongside overriding.

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.

Muhammad Hassan

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

回复

要查看或添加评论,请登录

Yamil Garcia的更多文章

社区洞察

其他会员也浏览了