Вы находитесь на странице: 1из 14

Function Overloading

21/01/11

Function overloading
The term refers to more than one function having the same name Overloaded function must have either different number of arguments or have arguments of different type.

Example
#include<iostream> using namespace std; int Product(int, int) ; int Product(int, int, int) ; double Product(int, double) ; int main() { int a = 5, b = 6, c = 2 ; double d = 5.5 ;

Contd..
cout << Product of a and b= <<Product(a,b)<<endl ; Cout<< Product of a, b and c= <<Product(a,b,c)<<endl ; Cout<< Product of b and d = <<Product(b,d)<<endl; return 0; } int Product(int x, int y)//definition of function {return x*y;} int Product(int n, int m, int k)//definition of function {return n*m*k; }

Contd..
double Product(int A, double B) //definition of //function {return A*B;} The expected output is given below: Product of a and b = 30 Product of a, b and c = 60 Product of b and d = 33

Inline Functions
The Inline keyword is used to declare an inline function. The use of inline function allows the compiler to substitute the called function body in the code itself, rather than transferring the control to the called function.

Example
#include<iostream> using namespace std; Inline int Power(int x, int n) //inline function { int P = 1; for(int i =0; i<n; i++) { P *= x ; return P ; }

Pointers in C++

Outlines
Declaration of Pointers Indirection or Dereference operator Processing DATA by using pointers Pointer to Pointer Pointer and Arrays Array of Pointers to Arrays Pointers to multi-dimensional arrays Pointers to Functions Array of Pointers to Functions Pointer to Functions as parameter of another function The new and delete Passing arguments through pointers

Pointers
Its a variable which holds the memory address of another variable The address of a variable is the address of the first byte of the memory block allocated to the variable. We can dig out the memory address of a variable by using address-of-operator( & ) also called reference operator.

Declaration of Pointers
By mentioning the type of the variable, followed by indirection operator, also called dereference operator (*), and the name of the pointer variable. The illustration of pointer declaration for int variables are given below : int m, n ; // m and n are integer variables int *ptr = &n ;//ptr is pointer to any integer number int* ptr = &m ; //ptr now points to n. int* ptr = 0 ; // now ptr is initialized to 0.

Pointers to different data type


float PI = 3.14159 ; float *pf = &PI ; float
pf

PI

points to char

3.14159

ch
A

char ch = A ; char *pc = &ch ;

pc

points to

double D = 4328.6543 double *pd = &D ; pd

double

points to

Indirection or Dereference Operator


The value of variable may be obtained from its pointer by using indirection operator also called dereference operator(*). It is called indirection because it obtains the value indirectly. An application of dereference operator is : int n = 60, *ptrn ; ptrn = &n ; *ptrn = 60 ; //use of dereference operator.

In detail we learn in next class

Вам также может понравиться