If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

In C++, difference between method overloading and method overriding?

Started by Bubunt, Jun 30, 2022, 09:20 AM

Previous topic - Next topic

BubuntTopic starter

In C++, method (or function) overloading refers to the capability of defining functions with the same name but different parameter sets.
On the other hand, method overriding pertains to the ability of a subclass to modify or rewrite the virtual method of its parent class.
  •  


span4bob

1. Overloading pertains to methods within the same class having a relationship, while overriding pertains to a relationship between a method in a superclass and the subclass.
2. Overloading does not prevent inheritance from the superclass, but overriding blocks inheritance.
3. In overloading, different methods share the same name, whereas in overriding, a subclass method replaces the superclass method.
4. Overloading requires different method signatures, whereas overriding requires the same signature.

Example:

1. Overriding

In the code example provided, a virtual method named "MyMethod()" is defined in the base class "MyBaseClass". The subclass "MyDerivedClass" then overrides this method by redefining it with the same name and signature, but with a different implementation.

2. Overloading

In the code example provided, two separate methods named "add" are defined, one that takes two integers as parameters, and another that takes two floats as parameters. These methods share the same name but have different parameter types, which allows them to be distinguished based on the argument types passed to them.
  •  
    The following users thanked this post: Bubunt

Michelangelos

Overrides in Obj-C and Swift behave similarly to Java, where Swift uses the "override" keyword for this feature.

The topic of overloading is often misunderstood. In Swift, methods that have the same name but different parameters are similar to Java, and the compiler determines the method to call based on the parameter type. However, when additional options are added, the method name changes accordingly, so it may not be considered an overload in the traditional sense. Yet Swift still uses the first part of the name (i.e. myFunction) in some cases as a way to differentiate between methods, so this could also be considered overloading, albeit with some uncertainty.

In contrast, Obj-C does not support overloading. When changing the number of parameters, the method name must also be changed. Note that when the method name is different, it is not considered an overload. However, plain C functions can still be overloaded in Obj-C.

Example:

In the sample code provided, it can be observed that in Swift, having methods with the same name but with different parameters is similar to Java. However, adding more options changes the method name, which may or may not be considered an overload in the traditional sense. Obj-C, on the other hand, does not support overloading, and changing the number of parameters also requires a change in the method name. Nonetheless, plain C functions can still be overloaded in Obj-C.
  •  

Enot

The main difference is that with a restore method, the compiler has the value of a particular object and knows the purpose of the class at compile time, while in a predefined method, nothing is known until runtime.
  •  

Peertan

Method overloading in C++ is akin to choreographing a symphony with multiple movements, each harmonizing distinct melodies. It entails the creation of multiple functions within the same scope, all bearing the same name but differing in their parameter lists. This elegant mechanism allows developers to encapsulate related operations under a unified name, amplifying code clarity and conciseness. Picture a versatile chef adept at customizing a dish based on the unique preferences of each diner, ensuring a delightful dining experience for all.

Conversely, method overriding in C++ unfolds as an artistic reinterpretation of a time-honored masterpiece. It grants a subclass the artful freedom to reimagine and refine a virtual method inherited from its parent class, infusing it with bespoke behavior. This concept mirrors the legacy of an artisan skillfully reshaping an heirloom, preserving its essence while infusing it with contemporary vitality. Through method overriding, software architects can tailor the behavior of a subclass to suit specific contextual needs, fostering adaptability and extensibility within the codebase.

In essence, the delicate dance of method overloading and overriding in C++ bestows upon developers the craftsmanship to craft articulate, evolvable, and refined software solutions that resonate with the ever-evolving cadence of technological progress.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...