Must I need to redefine all the overloading operators with derived type if I require to use them in derived class?
The following code compiles fine:
In C++, there is no overloading across scopes derived class scopes are not an exception to this general rule.
There is no overload resolution between Derived and Base class. An example:
class B
{
public:
int func1(int i)
{
cout<<"B::func1()";
return i+1;
}
};
class D : public B
{
public:
double func1(double d)
{
cout<<"D::func1()";
return d+1.3;
}
};
int main ()
{
D *pd = new D;
cout << pd->func1(2) <func1(2.3)<
The output is:
D::func1()3.3
D::func1()3.6
This same rule applies for operator member functions as well, after all they are member functions too!
So in your code example if Point had more than one operator+(), and you redefined the same operator in Derived class then only that derived class operator will be accessible to objects of derived class because that version of the function hides the other Base class versions of operator+().
If you do not redefine the operator+() in the derived class, then none of the parent class versions of the operator+() are hidden and hence accessible through objects of Derived class.
Hence the statement:
If a derived class wants to make all the overloaded versions available through its type, then it must either redefine all of them or none of them.
Also, please note that overloading, overriding and function hiding are three terms that are loosely mis-used interchangeably sometimes but they all have separate meanings.