Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overloading vs overriding

I am a little confused over the two terminologies and would be glad to get some doubts clarified.

As I understand function overloading means having multiple methods in the same class with same name but either with different number of arguments, different types of arguments or sequence of arguments irrespective of the return type which has no effect in mangled name of the functions.

Does the above definition also include "....in the same class or across related classes(related through inheritance)....."

And Function Overriding is related to virtual functions, same method signature(declared virtual in Base class) and overridden for implementation in Sub Classes.

I was wondering at a scenario, following is the code:

#include <iostream>

class A
{
    public:
    void doSomething(int i, int j)
    {
        cout<<"\nInside A::doSomething\n";
    }
};

class B: public A
{
    public:
    void doSomething(int i, int j)
    {
        cout<<"\nInside B::doSomething\n";

    }
};

int main()
{
    B obj;
    obj.doSomething(1,2);
    return 0;

} 

In the above scenario, What can be said:
The method in derived class overrides method in Base class OR
The method in derived class overloads method in Base Class

Does Overloading apply across class scopes and does overriding term doesn't necessarily apply to virtual functions?

I think it should be overrides, but just need the clarification because i happen to remember the term overriding being used specifically with virtual functions.

like image 696
Alok Save Avatar asked Mar 23 '11 14:03

Alok Save


People also ask

Which is better overriding or overloading?

Performance: Overloading gives better performance compared to overriding. The reason is that the binding of overridden methods is being done at runtime. private and final methods can be overloaded but they cannot be overridden.

What is the difference between polymorphism overloading and overriding?

In case of method overloading, parameter must be different. In case of method overriding, parameter must be same. Method overloading is the example of compile time polymorphism. Method overriding is the example of run time polymorphism.


2 Answers

  • hiding is when a definition in a scope is not accessible due to a declaration in a nested scope or a derived class (3.3.7/1).

A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class.

  • overriding is when a virtual member is replaced in a derived class (see 10.3/2)

If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name and same parameter list as Base::vf is declared, then Derived::vf is also virtual an it overrides Base::vf.

  • overloading is when several declarations coexist for the same name in the same scope (13/1)

When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded.

  • related, there is also the possibility of replacing operator new and delete from the standard library by one's own implementation (18.4.1.1/2, 18.4.1.1/6, 18.4.1.1/11, 18.4.1.2)

So this is clearly a case of hiding.

like image 149
AProgrammer Avatar answered Oct 14 '22 06:10

AProgrammer


In this case neither. The derived-class method hides the base-class method.

like image 40
Björn Pollex Avatar answered Oct 14 '22 06:10

Björn Pollex