View study notes of Function overloading .you can also visit Tutorialfocus.net to get complete description step wise of the concerned topic.Other topics and notes of C++ are also explained.
2. 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 Ritika sharma
3. 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 Ritika sharma
4. 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 Ritika sharma
5. 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 Ritika sharma
6. Function Overloading
ïTwo or more functions can have the same name but different
parameters
ïExample:
int max(int a, int b)
{
if (a>= b)
return a;
else
return b;
}
float max(float a, float b)
{
if (a>= b)
return a;
else
return b;
}
6 Ritika sharma
7. 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 Ritika sharma
8. 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)
{
cout<<ân sum of characters areâ<<x+y;
8 Ritika sharma
11. 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 Ritika sharma
12. ï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 Ritika sharma
14. Scope
ïThe scope of a variable is the portion of a program where the
variable has meaning (where it exists).
ïA global variable has global (unlimited) scope.
ïA local variableâs scope is restricted to the function that
declares the variable.
ïA block variableâs scope is restricted to the block in which
the variable is declared.
14 Ritika sharma
15. Understanding Scope
ïSome variables can be accessed throughout an entire
program, while others can be accessed only in a limited part
of the program
ïThe scope of a variable defines where it can be accessed in a
program
ïTo adequately understand scope, you must be able to
distinguish between local and global variables
15 Ritika sharma
16. Local variables
ïParameters and variables declared inside the definition of a
function are local.
ïThey only exist inside the function body.
ïOnce the function returns, the variables no longer exist!
ïThatâs fine! We donât need them anymore!
16 Ritika sharma
17. Block Variables
ïYou can also declare variables that exist only within the body
of a compound statement (a block):
{
int foo;
âŠ
âŠ
}
17 Ritika sharma
18. Global variables
ïYou can declare variables outside of any function definition â
these variables are global variables.
ïAny function can access/change global variables.
ïExample: flag that indicates whether debugging information
should be printed.
18 Ritika sharma
19. Distinguishing Between Local
and Global Variables
ïCelebrity names are global because they are known to people
everywhere and always refer to those same celebrities
ïGlobal variables are those that are known to all functions in a
program
ïSome named objects in your life are local
ïYou might have a local co-worker whose name takes
precedence over, or overrides, a global one
19 Ritika sharma
20. A note about
Global vs. File scope
ïA variable declared outside of a function is available
everywhere, but only the functions that follow it in the file
know about it.
ïThe book talks about file scope, Iâm calling it global scope.
20 Ritika sharma
21. Block Scope
int main(void) {
int y;
{
int a = y;
cout << a << endl;
}
cout << a << endl;
}
Error â a
doesnât exist outside
the
block!
21 Ritika sharma
Hinweis der Redaktion
Multiple function with same name and same number of parameters differ only in data types