0% found this document useful (0 votes)
140 views

Scope) That Are Differentiable by The Number or Types of Their

Function overloading allows multiple functions with the same name but different parameters. It is achieved by having different signatures based on number and type of arguments. The compiler performs argument matching to resolve overloading. Certain rules apply - functions differing only in return type cannot be overloaded, static and non-static member functions cannot be overloaded if their parameters are the same. Typedefs are not considered different types for overloading. Signedness, const-ness, and enumerated types can distinguish signatures.

Uploaded by

Praveen Gulati
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views

Scope) That Are Differentiable by The Number or Types of Their

Function overloading allows multiple functions with the same name but different parameters. It is achieved by having different signatures based on number and type of arguments. The compiler performs argument matching to resolve overloading. Certain rules apply - functions differing only in return type cannot be overloaded, static and non-static member functions cannot be overloaded if their parameters are the same. Typedefs are not considered different types for overloading. Signedness, const-ness, and enumerated types can distinguish signatures.

Uploaded by

Praveen Gulati
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

What is function overloading?

A function name having several definitions (or signatures) (in same


scope) that are differentiable by the number or types of their
arguments is known as overloading of function.
For example
float area(float a);
float area(double a);
area function is said to be overloaded.
What is signature of function?
A functions argument list (i.e. number and type of arguments) is
known as the functions signature.
What is extended name of function
Extended name of function means including argument list in name
of function to distinguish one function signature from other.
So for function
float area(double a);
area is name of function but area(double) is extended name.
What is meant by same scope in above defn?
Suppose two independent separate classes abc and xyz both have
function with same name area, it is not called overloaded function,
as these are in different classes.
If two signatures are taking different number of arguments, it is
overloaded.
If number of arguments is same, then at least one argument must
have different type
funca is overloaded as number of arguments is 2 and 3
Void funca(int a, int b, int c);
void funca(int a, char b, int c);
What is need for function overloading?
To behave differently in different situations (probably same
behavior), for example area calculation can use different formula for
different situation (for example rectangle and square).
float area(double length, double breadth);
// for rectangle
float area(double length);
// for square
Here behavior is same, which is calculation of area.
So basically function overloading implements polymorphism.
Cant this be achieved by other means?
Yes, for example you can use different names or default arguments.
But using different names means programmer needs to remember
more methods. And default arguments approach has its own
limitations like intermediate arguments alone cant be default
arguments.

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

error is thrown. So compiler must find either exact one match or


single un-ambiguous best match.

Order of matching (in function overloading)


Matching is performed in following order
a. Exact match (example int to int)
b. A match through promotion (example char -> int)
c. Matching using standard C++ conversion rules (int ->
double)
d. Matching using user defined conversions and built in
conversions
In above steps if a match is found then next step is not performed.
Is function overloading applicable
a) to normal functions
b) to member functions (functions belonging to classes)
c) both
[Answer]
Usage of constant suffixes to avoid ambiguity
Constant suffixes like (F, L, U, UL, etc.) can be used to distinguish
multiple ambiguous match.
Default constant matching
34
int
(to be more precise it is signed int)
34.0
double
Case 1
float area(int);
float area(double);
float area(long);
float(34) is allowed
as it has exact match
even if there multiple
matches other than
exact match, it is not
an error.

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

Certain function declarations cannot be overloaded:

Function declarations that differ only in the return type cannot be


overloaded.

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

Enumerated types can be used to distinguish two different functions for


overloading purpose.
Example A 24
enum color {
BLACK,
YELLOW
};
enum gender {
MALE,
FEMALE
};
class printer {
public:
void printdescription(color);
void printdescription(gender);
};

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);
};

Below will be an error


class kjkds {
public:
void func(const int a, int b, signed int c);
void func(const int a, int b, int c);
};
as int by default is signed int so both signatures will actually become same

const-ness (Allowed as per book)

class fxyaz {
public:
void func(const int a, int b, signed int c);
void func(int a, int b, signed int c);
};

A class can have an overloaded constructor


A destructor function can not be overloaded.
Solved Problems Page SA Book 123,

You might also like