views:

84

answers:

1

hello, can anyone give me an example of function overloading in c++ with 4 function prototypes ? i still don't get them quite good ..

sorry newbie question, thanks for looking in.

Adam Ramadhan

+5  A: 

The following are C++ function declarations and would typically be in the header (.h or .hpp) file. These particular declarations have no code. The code is in the definition shown further below.

int sum(int a, int b);
int sum(int a, int b, int c);
int sum(int a, int b, int c, int d);
int sum(int a, int b, int c, int d, int e);

The above four functions have the same name, but the C++ compiler will call the one whose parameter signature matches the one in the calling code. The purpose of a declaration is to tell the compiler what the return type and parameter signature of a function are. If more than one function has the same name but differs in its parameter signature, it is referred to as overloaded. This is a C++ feature not present in C. Note that the return type cannot be used to differentiate overloaded functions.

The following are the definitions (implementations) of the overloaded functions and would typically be in the module (.cpp or .cc or .cxx) file. This is where the executable code resides between the braces { } that surround the function block:

int sum(int a, int b)
{
    return (a + b);
}

int sum(int a, int b, int c)
{
    return (a + b + c);
}

int sum(int a, int b, int c, int d)
{
    return (a + b + c + d);
}

int sum(int a, int b, int c, int d, int e)
{
    return (a + b + c + d + e);
}

Usage example:

std::cout << sum(3, 4, 5) << std::endl;

will invoke the code for the second overloaded function listed above that takes three int parameters.

Amardeep
and the function ? is a prototype function is a function ?..
Adam Ramadhan
I'm editing my answer to better explain that aspect.
Amardeep
ok im looking stupid. sorry
Adam Ramadhan
so there are no prototypes in C++ ? olny in c ? how do we do the second one in C ? do we put them two ?
Adam Ramadhan
ah i understand about overflow concept ok. now its about the prototypes, im still a bit confused, so prototype = declaration ? like function x( arg1, arg2) ? is this a prototype ? what is the diffrence between normal function in c ? and in c++ ? i still just dont get it.
Adam Ramadhan
@Adam it's not defined in C++ what a "prototype" is. Collegially, a function prototype is a function declaration though. It's only defined in C, in which it is the information about a function parameter count and types. It's not true that `void f() { }` would not be a declaration: It's both a declaration and definition. It's important to keep this concept distinct from "signature". The signature of a function can be seen as its identity: Roughly its scope (the class or namespace it's a member of), name and type. For template functions, the signature contains further stuff.
Johannes Schaub - litb
@Amardeep your last 3 function definitions have extra `;` in them.
Bruce
@Bruce: Thanks! Cut-n-paste error.
Amardeep