Scope) That Are Differentiable by The Number or Types of Their
Scope) That Are Differentiable by The Number or Types of Their
Re-declaration of a function
A function can be declared multiple times in a program, provided its
signature is same. For example
int funca(int b, int y);
int area(int c) {
//..some code
};
int funca(int b,
int y);
// this is re-declaration of funca, and is
allowed
Variable names in re-declaration
It is allowed.
void funcb(int x, int y);
void funcb(int a, int y);
(notice x and a are variable names and not types)
Multiple definitions
If you try to define a function twice, compiler will give error
For example following code is not allowed even if code1 and code 2
is same
int funca(int b, int y) {
// code1
};
int area(int c) {
//..some code
};
int funca(int c, int y) {
// some code2
}
Re-declaration error
long funca(int a, int b);
int funca(int a, int b);
// error
Function overloading does not allow two declarations with same
argument list but different return type alone (argument list means
number of arguments and type of arguments)
compiler treats as if you are trying to re-declare same function
differently and throws re-declaration error.
Argument Matching (Process of Disambiguation)
Each function call is matched to its appropriate definition. This
involves comparing each argument in function call to the formal
argument of the functions signature. If no match is found then error
is thrown, also in case multiple ambiguous matches are found then
Case 2
float area(double);
float area(long);
Case 3
float area(double);
float area(long);
As there is no
method which takes
int (exact match, and
int can be converted
to double or long it is
an error of multiple
ambiguous matches.
float(34.0) or
float(34L) or
float(34F) can be
used to call function
without errors
Member function declarations with the same name and the same
parameter types cannot be overloaded if any of them is a static member
function declaration
Two argument types created using typedef declarations are considered
same. They are just synonyms for existing type. (Example A23)
Example A21
class number {
public:
number(int i) { num = i;}
static int square(int b);
int square(int j);
private :
int num;
};
Error :
// Static and non-static member functions
with the same parameter types cannot be overloaded
Reason
We are trying to over load square method just based on
difference of static and not static
Example A22
following is allowed and valid code
class number {
public:
number(int i) { num = i;}
static int square(int b);
int square(int j);
private :
int num;
};
Reason
Although one square method is static and one non-static but argument list is
different so extended name of function is actually different.
Example A23
typedef int Integer;
class shape {
public:
float area(int a);
float area(Integer a);
};
Example A23 Error Class member cannot be redeclared
Signed-ness
Argument unsigned and signed are treated as different signature.
class kjkds {
public:
void func(const int a, int b, unsigned int c);
void func(const int a, int b, int c);
};
class fxyaz {
public:
void func(const int a, int b, signed int c);
void func(int a, int b, signed int c);
};