Presentation is loading. Please wait.

Presentation is loading. Please wait.

Function Overloading Can enables several functions Of same name Of different sets of parameters (at least as far as their types are concerned) Used to.

Similar presentations


Presentation on theme: "Function Overloading Can enables several functions Of same name Of different sets of parameters (at least as far as their types are concerned) Used to."— Presentation transcript:

1 Function Overloading Can enables several functions Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create several functions of the same name that perform similar tasks but on different data types

2 Functions have same name, same number of parameters, but parameters are of different data types. Following square functions are example of this type of overloading. Each function has name square, one parameter and return data. However, the first one is for integer, the second is for double and the third is for float

3 Square function int square(int x) { return x*x; } double square(double y) { return y*y;} float square(float z) { return z*z;}

4 Use overloaded function Complier will search for the match function prototype int main() { int x=7; double y=7.5; cout<<“square of “<<x<<“ is “ <<square(x)<<endl; cout<<“Square of “<<y<<“is “ <<square(y)<<endl; }

5 Another type of overloading Functions have same name, but different number of parameters Time class constructors are one example Time(); - has no parameter Time(int h, int m, int s) ; - take three parameters

6 Another example Define a print function that print an array of integer prototype void print( int array[], int size); Function definition void print( int array[], int size) { for( int I = 0; I <size; I ++) { cout<< array[I] <<“ “; if( I%5 == 0 ) cout<<endl; }

7 Define print function Define print function that print array of float Prototype void print( float array[], int size); Function definition void print( float array[], int size) {for( int I = 0; I <size; I ++) { cout<< array[I] <<“ “; if( I%5 == 0 ) cout<<endl; }

8 Create a project and Define overloading print functions Define print function that prints array of integer with given starting and ending index void print( int array[], int size, int start, int end); The function should print out the array elements from index start to index end. The function should print out message if the start and end are beyond the array range.

9 Define the following functions void print( int array[], int size); void print( float array[], int size); void print( double array[], int size); void print( int array[], int size, int start, int end); void print( float array[], int size, int start, int end); void print( double array[], int size, int start, int end);

10 Test your function with the main program int main() { int a[10] = {1,2,3,4,5,6,7,8,9,10}; float b[5] = {1.1,2.2,3.3,4.4,5.5}; double c[3] = { 1.123,2.234,3.345}; print( a, 10); print(b,5); print(c, 3); print (a, 10, 3,5); print (b, 5,1,4); print ( c, 3,0,1); return 0; }


Download ppt "Function Overloading Can enables several functions Of same name Of different sets of parameters (at least as far as their types are concerned) Used to."

Similar presentations


Ads by Google