Yes, I do understand the difference between them. What I want to know is: why OVERRIDE a method? What is the good in doing it? In case of overload: the only advantage is you
People already defined both overloading and overriding, so I won't elaborate.
ASAFE asked:
the only advantage [to overloading] is you haven't think in several names to functions?
And this is already a mighty advantage, isn't it?
Let's compare with known C API functions, and their fictional C++ variants:
/* C */
double fabs(double d) ;
int abs(int i) ;
// C++ fictional variants
long double abs(long double d) ;
double abs(double d) ;
float abs(float f) ;
long abs(long i) ;
int abs(int i) ;
This means two things: One, you must tell the compiler the type of the data it will feed to the function by choosing the right function. Two, if you want to extend it, you'll need to find fancy names, and the user of your functions will have to remember the right fancy names.
And all he/She wanted was to have the absolute value of some numerical variable...
One action means one and only one function name.
Note that you are not limited to change the type of one paramter. Anything can change, as long as it makes sense.
Let's see of operators:
// C++
Integer operator + (const Integer & lhs, const Integer & rhs) ;
Real operator + (const Real & lhs, const Real & rhs) ;
Matrix operator + (const Matrix & lhs, const Matrix & rhs) ;
Complex operator + (const Complex & lhs, const Complex & rhs) ;
void doSomething()
{
Integer i0 = 5, i1 = 10 ;
Integer i2 = i0 + i1 ; // i2 == 15
Real r0 = 5.5, r1 = 10.3 ;
Real r2 = r0 + r1 ; // r2 = 15.8
Matrix m0(1, 2, 3, 4), m1(10, 20, 30, 40) ;
Matrix m2 = m0 + m1 ; // m2 == (11, 22, 33, 44)
Complex c0(1, 5), c1(10, 50) ;
Complex c2 = c0 + c1 ; // c2 == (11, 55)
}
In the above example, you do want to avoid using anything else than the + operator.
Note that C has implicit operator overloading for built-in types (including C99 complex type):
/* C */
void doSomething(void)
{
char c = 32 ;
short s = 54 ;
c + s ; /* == C++ operator + (char, short) */
c + c ; /* == C++ operator + (char, char) */
}
So even in non-object languages, this overloading thing is used.
Let's see the use of an object basic methods: Its constructors:
class MyString
{
public :
MyString(char character) ;
MyString(int number) ;
MyString(const char * c_style_string) ;
MyString(const MyString * mySring) ;
// etc.
} ;
Some could consider this like function overloading, but in fact, it is more similar to operator overloading:
void doSomething()
{
MyString a('h') ; // a == "h" ;
MyString b(25) ; // b == "25" ;
MyString c("Hello World") ; // c == "Hello World" ;
MyString d(c) ; // d == "Hello World" ;
}
In C, when you give the name of the function, the parameters are implicitely part of the signature at call. If you have "double fabs(double d)", then while the signature of fabs for the compiler is the undecorated "fabs", it means that you must know it takes only doubles.
In C++, the name of the function does not mean its signature is forced. Its signature at call is its name and its parameters. Thus, if you write abs(-24), the compiler will know what overloading of abs it must call, and you, when writing it, find it more natural: You want the absolute value of -24.
Anyway, anyone who coded somewhat in any language with operators already uses overloading, be it C or Basic numerical operators, Java string concatenation, C# delegates, etc.. Why? because it's more natural.
And the examples shown above are just the tip of the iceberg: When using templates, overloading become very very useful, but this is another story.