In C++, what is the difference between method overloading and method overriding?

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

Previous topic - Next topic

BubuntTopic starter

Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters).
Method overriding is the ability of the inherited class rewriting the virtual method of the base class.
  •  

span4bob

1.In overloading,there is a relation ship between methods available in the same class where as in overridding,there is relationship between a super class method and subclass method.
2.Overloading doesn't block inheritence from the superclass where as overridding blocks inheritence.
3.In overloading,seperate methods share the same name where as in overridding,subclass methods replaces the superclass.
4.Overloading must have different method signatures where as overriding must have same signature.

Example

1. Overriding
public class MyBaseClass
{
    public virtual void MyMethod()
    {
        Console.Write("My BaseClass Method");

    }
}
public class MyDerivedClass : MyBaseClass
{
    public override void MyMethod()
    {
        Console.Write("My DerivedClass Method");

    }
}

2. Overloading
int add(int a, int b)
int add(float a , float b)
  •  
    The following users thanked this post: Bubunt

Michelangelos

Overriding in Obj-C and Swift works just like it does in Java (in Swift, it's even denoted by the override keyword).

Obviously, there is a lot of confusion about overloading. Let's start with Swift. In Swift in general, this works the same way as in Java:

func myFunction(a: Int)
func myFunction(a: String)

The same method name that takes different parameters. The compiler will decide which method to call based on the type of the parameter. However, when adding more options:

func myFunction(a: Int, b: Int)
the name of the method actually changes from myFunction(_:) to myFunction(_:b:), so it shouldn't be called an overload in the traditional sense, but I guess Swift only uses the first part of the name (myFunction) in some cases to differentiate between methods, so this can be an overload too. I'm a little unsure in this case.

There is no overload in Obj-C.

We cannot declare the following in Obj-C:

@interface MyClass : NSObject

- (void)myMethod:(id)parameterA;
- (void)myMethod:(NSInteger)parameterA;

@end

and when changing the number of parameters, we also have to change the method name

@interface MyClass : NSObject

- (void)myMethod:(id)parameterA;
- (void)myMethod:(NSInteger)parameterA withAnotherParam:(id)parameterB;

@end

Note that the method names are -myMethod: and -myMethod:withAnotherParam: respectively. When the method name is different, it is not overloaded.

However, even in Obj-C we can use plain old C functions and they can be overloaded:

void functionA(int a) {
  ...
}

void functionA(int a, int b) {
  ...
}
  •  

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.
  •