Search code examples
c++overridingoverloading

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.


Solution

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