I don't really understand it that well, can someone explain it or give me a link to a good tutorial, I'd really appreciate it. A sample code of how overloaded functions work would also be nice.
Printable View
I don't really understand it that well, can someone explain it or give me a link to a good tutorial, I'd really appreciate it. A sample code of how overloaded functions work would also be nice.
Function overloading allows you to choose the same name for functions, and give them different signatures ("signature" refers to argument list). This allows the compiler to choose which "version" of a particular function to use based on the arguments passed to it.
For example:
int func(int i, char *string);
double func(char *string);
int func(char code);
Careful of ambiguosity, something like this is quite confusing:
int max(int i1, int i2 = 0);
int max(int i1);
Calling max(4); will generate an error, as the function call matches two function signatures.
In addition to what Elbro said you can use overloading to overload operators for user defined datatypes, which is also very useful.
Is this also known as Polymorphism????
No that would be something like this
// Dog is ofcourse derived from mamel
Mamel *theDog = new Dog;
I thought that Polymorphism was defined as having functions with the same names but different funtionalities such as
Code:door.open();
account.open();
When I think of polymorphism I think of a function with the same name and the same parameters, but different response based on the class that it is being used in. See the speak() function above. Thus it polyorphism is similar to overloading--the functions have the same name but different output for in both-- but different as well--polymorphic functions have the same parameters whereas overloaded functions don't.Code:class animal
{
public:
virtual void speak() {};
};
class dog : class animal
{
public:
void speak() {cout << "bow wow" << endl;}
};
class cat : class animal
{
public:
void speak() {cout << "prrrrrr...." << endl;}
};