03 function overloading

18
FUNCTION OVERLOADING Subject: Programming in C++ By Jasleen Kaur Assistant Professor (CSE)

Transcript of 03 function overloading

Page 1: 03 function overloading

FUNCTION OVERLOADING

Subject: Programming in C++

By Jasleen Kaur

Assistant Professor (CSE)

Page 2: 03 function overloading

Function Overloading• If any class have multiple functions with same

names but different parameters then they are saidto be overloaded. Function overloading allows youto use the same name for different functions, toperform, either same or different functions in thesame class.

• If you have to perform one single operation but withdifferent number or types of arguments, then youcan simply overload the function.

Page 3: 03 function overloading

Way to Overload a Function

• By changing number of Arguments.

• By having different datatypes of argument.

Page 4: 03 function overloading

• When an overloaded function is called, theC++ compiler selects the proper function byexamining the number, types and order ofarguments in the function call.

• Function overloading is commonly used tocreate several functions of same name thatperform similar tasks but on data of differenttypes.

Page 5: 03 function overloading

Number of Arguments different• In this, we can define two functions with same names but different

number of parameters of the same type. For example, in the belowmentioned program we have made two sum() functions to returnsum of two and three integers.

• Example

int sum (int x, int y)

{

cout << x+y;

}

Here sum() function is overloaded, to have two and threearguments. Which sum() function will be called, depends on thenumber of arguments.

int sum(int x, int y, int z){

cout << x+y+z;}

Page 6: 03 function overloading

Different Datatype of Arguments

• In this type of overloading we define two ormore functions with same name and samenumber of parameters, but the type ofparameter is different. For example in thisprogram, we have two sum() function, first onegets two integer arguments and second onegets two double arguments.

Page 7: 03 function overloading

Exampleint sum(int x,int y)

{

cout<< x+y;

}

double sum(double x, double y)

{

cout << x+y;

}

int main( )

{

sum (10,20);

sum(10.5,20.5);

}

Page 8: 03 function overloading

WAP to overload square function whichcalculate square of an int and square ofdouble.

Question for Practice

Page 9: 03 function overloading

#include<iostream.h>

#include<conio.h>

int square(int x)

{

cout<<“square of integer”<<x<<“is”;

return x*x;

}

double square(double y)

{

cout<<“square of double”<<y<<“is”;

return y*y;

}

Page 10: 03 function overloading

void main()

{

cout<<square(7);//calls int version

cout<<endl;

cout<<square(7.5); //calls double version

cout<<endl;

getch();

}

Page 11: 03 function overloading

How Compiler differentiates overloaded functions?

• Overloaded functions are distinguished bytheir signatures.

• A signature is a combination of function’sname and its parameter types(in order).

• Compiler encodes each function identifierwith the number and types of its parameters.This is sometimes referred to as namemangling.

Page 12: 03 function overloading

• The compiler uses only parameter list todistinguish between functions of same name.

How Compiler differentiates overloaded functions?

Page 13: 03 function overloading

void f(int x);

void f(int &x); // error

Two functions cannot be overloaded when theonly difference is that one takes a referenceparameter and the other takes a normal, call-by-value parameter.

Page 14: 03 function overloading

Functions can be distinguished on the basis of return type, they cannot be overloaded on this basis.

Page 15: 03 function overloading

Function with Default Arguments• When we mention a default value for a parameter

while declaring the function, it is said to be as defaultargument. In this case, even if we make a call to thefunction without passing any value for thatparameter, the function will take the default valuespecified.

• Example

sum(int x,int y=0)

{

cout << x+y;

}

Page 16: 03 function overloading

Cont..• Here we have provided a default value for y, during function

definition.• Exampleint main( ) { sum(10);sum(10,0); sum(10,10);}• Output : 10 10 20 First two function calls will produce the exact

same value, for the third function call, y will take 10 as valueand output will become 20.

• By setting default argument, we are also overloading thefunction. Default arguments also allow you to use the samefunction in different situations just like function overloading.

Page 17: 03 function overloading

Rules for using Default Arguments

• Only the last argument must be given defaultvalue. You cannot have a default argumentfollowed by non-default argument.

• sum (int x, int y);

• sum (int x, int y=0);

• sum (int x=0, int y); // This is Incorrect

Page 18: 03 function overloading

Rules for using Default Arguments

• If you default an argument, then you will have to default all the subsequent arguments after that.

– sum (int x,int y=0);

– sum (int x,int y=0,int z); // This is incorrect

– sum (int x,int y=10,int z=10); // Correct

• You can give any value a default value to argument, compatible with its datatype.