0% found this document useful (0 votes)
23 views12 pages

Dokumen - Tips Function Overloadingc

Uploaded by

Nabeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views12 pages

Dokumen - Tips Function Overloadingc

Uploaded by

Nabeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

FUNCTION

OVERLOADING

1 Nabeel
Polymorphism
The word polymorphism is derived from Greek word
Poly which means many and morphos which means
forms.
Polymorphism can be defined as the ability to use the
same name for two or more related but technically
different tasks.
Eg-woman plays role of daughter,sister,wife,mother
etc.

2 Nabeel
Overloading in C++
 What is overloading
– Overloading means assigning multiple
meanings to a function name or operator
symbol
– It allows multiple definitions of a function with the
same name, but different signatures.
 C++ supports
– Function overloading
– Operator overloading

3 Nabeel
Why is Overloading Useful?
 Function overloading allows functions that
conceptually perform the same task on
objects of different types to be given the
same name.

 Operator overloading provides a convenient


notation for manipulating user-defined
objects with conventional operators.

4 Nabeel
Function Overloading
Is the process of using the same name for two or more
functions
Requires each redefinition of a function to use a
different function signature that is:
different types of parameters,
or sequence of parameters,
or number of parameters
Is used so that a programmer does not have to
remember multiple function names

5 Nabeel
Function Overloading
Two or more functions can have the same name but
different parameters
Example:

int max(int a, int b) float max(float a, float b)


{ {
if (a>= b) if (a>= b)
return a; return a;
else else
return b; return b;
} }

6 Nabeel
Overloading Function Call Resolution
 Overloaded function call resolution is done by
compiler during compilation
– The function signature determines which definition
is used
 a Function signature consists of:
– Parameter types and number of parameters
supplied to a function
 a Function return type is not part of function
signature
and is not used in function call resolution
7 Nabeel
void sum(int,int);
void sum(double,double);
void sum(char,char);
void main()
{
int a=10,b=20 ;
double c=7.52,d=8.14;
char e=‘a’ , f=‘b’ ;
sum(a,b); //calls sum(int x,int y)
sum(c,d); //calls sum (double x,double y)
sum(e,f); // calls sum(char x,char y)
}
void sum(int x,int y)
{
vout<<“\n sum of integers are”<<x+y;
}
void sum(double x,double y)
{
cout<<“\n sum of two floating no are”<<x+y;
}
void sum(char x,char y)
{ Nabeel
8
cout<<“\n sum of characters are”<<x+y;
Output:
Sum of integers 30
sum of two floating no are 15.66
sum of characters are 195

9 Nabeel
Void area(int)
Void area(int,int);
Void area(int,int,int);
Int main()
{
Int side=10,le=5,br=6,a=4,b=5,c=6;
Area(side);
Area(le,br);
Area(a,b,c);
Getch();
Return 0;
}
Void area(int x)
{ cout<<“area is”<<x*x;
}
Void area(int x,int y)
{cout<<“area of rectang;e”=<<x*y;
}
Void area(int x,int y,int z)
{cout<<“volume is”<<x*y*z;
}
10 Nabeel
Function Selection Involves following
Steps.
Compiler first tries to find the Exact match in which
the type of argument are the same,and uses that func.
If an exact match is not found,the compiler user the
integral promotions to the actual argument such
as,char to int, float to double.
When either of them fails ,build in conversions are
used(implicit conversion) to the actual arguments and
then uses the function whose match is unique.but if
there are multiple matches,then compiler will generate
an error message.

11 Nabeel
For ex: long square(long n)
long square(double x)
Now a func. call such as square(10) will cause
an error because int argument can be
converted into long also and double also.so it
will show ambiguity.

User defined conversion are followed if all the


conversion are failed.

12 Nabeel

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy