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 mult
In this case neither. The derived-class method hides the base-class method.
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:
void f(int);
void f(char);
class some_class {
void g();
void g() const;
};
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:
class base {
void f();
virtual void g();
};
class derived : public base {
void f();
void g();
};
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:
class base {
void f(int);
void f(char);
};
class derived1 : public base {
void f(double);
};
void f()
{
derived1 d;
d.f(42); // calls derived1::f(double)!
}
class derived2 : public base {
using base::f; // bring base class versions into derived2's scope
void f(double);
};
void g()
{
derived2 d;
d.f(42); // calls base::f(int)!
}
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.
class Test {
// Test::func is overloaded
virtual void func(int x);
virtual void func(double y);
};
class Child : public Test {
// Child::func overrides Test::func
virtual void func(int x);
};
A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class.
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.
When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded.
So this is clearly a case of hiding.