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.
Function overloading is when you have several functions which differ in their parameter list or, if they are member functions, in their
const
/volatile
qualification. (In some other languages you can also overload based on the return type, but C++ doesn't allow that.)Examples:
Function overriding is when you redefine a base class function with the same signature. Usually this only makes sense if the base class function is virtual, because otherwise the function to be called (base or derived class' version) is determined at compile-time using a reference's/pointer's static type.
Examples:
Function hiding is when you define a function ina derived class (or an inner scope) that has a different parameter list than a function with the same name declared in a base class (or outer scope). In this case the derived class' function(s) hides the base class function(s). You can avoid that by explicitly bringing the base class function(s) into the derived class' scope with a
using
declaration.Examples:
Just in case it's unclear: Based on these definitions, I'd call the scenario in question here overriding.
Overloading is the process of defining multiple methods with identical names but different signatures; Overriding is when a function in a child class has an identical signature to a virtual function in a parent class.
In this case neither. The derived-class method hides the base-class method.
So this is clearly a case of hiding.