Classes function overloading

17

Click here to load reader

Transcript of Classes function overloading

Page 1: Classes function overloading

OOPS

DONE BY:Ankush Kumar

Page 2: Classes function overloading

Function OverloadingC++ permits the use of two function with the same name.

However such functions essentially have different argument list. The difference can be in terms of number or type of arguments or

both.The biggest advantage of overloading is that it helps us to

perform same operations on different datatypes without having the need to use separate names for each version.

This process of using two or more functions with the same name but differing in the signature is called function overloading.

But overloading of functions with different return types are not allowed.

In overloaded functions , the function call determines which function definition will be executed.

Page 3: Classes function overloading

Function OverloadingExample:

#include<iostream>using namespace std;

int abslt(int );long abslt(long );float abslt(float );double abslt(double );

int main(){ int intgr=-5; long lnt=34225; float flt=-5.56; double dbl=-45.6768; cout<<\" absoulte value of \"<<intgr<<\" = \"<<abslt(intgr)<<endl; cout<<\" absoulte value of \"<<lnt<<\" = \"<<abslt(lng)<<endl;

Page 4: Classes function overloading

cout<<\" absoulte value of \"<<flt<<\" = \"<<abslt(flt)<<endl;cout<<\" absoulte value of \"<<dbl<<\" = \"<<abslt(dbl)<<endl;}int abslt(int num){if(num>=0)return num;else return (-num);}long abslt(long num){if(num>=0)return num;else return (-num);}float abslt(float num){if(num>=0)return num;else return (-num);}double abslt(double num)

Page 5: Classes function overloading

if(num>=0)return num;else return (-num);}

OUTPUT absoulte value of -5 = 5 absoulte value of 34225 = 34225 absoulte value of -5.56 = 5.56 absoulte value of -45.6768 = 45.6768

The above function finds the absolute value of any number int, long, float ,double.

The use of overloading may not have reduced the code complexity /size but has definitely made it easier to understand and avoided the necessity of remembering different names for each version function which perform identically the same task.

Page 6: Classes function overloading

Call by Value & Call by Reference

In C ++ programming language, variables can be referred differently depending on the context. For example, if you are writing a program for a low memory system, you may want to avoid copying larger sized types such as structs and arrays when passing them to functions. On the other hand, with data types like integers, there is no point in passing by reference when a pointer to an integer is the same size in memory as an integer itself.

Now, let us learn how variables can be passed in a C program.

Page 7: Classes function overloading

Call By Value

When you use pass-by-value, the compiler copies the value of an argument in a calling function to a corresponding non-pointer or non-reference parameter in the called function definition. The parameter in the called function is initialized with the value of the passed argument. As long as the parameter has not been declared as constant, the value of the parameter can be changed, but the changes are only performed within the scope of the called function only; they have no effect on the value of the argument in the calling function.

In the following example, main passes func two values: 5 and 7. The function func receives copies of these values and accesses them by the identifiers a and b. The function func changes the value of a. When control passes back to main, the actual values of x and y are not changed.

Page 8: Classes function overloading

Sample Program#include <stdio.h>

void func (int a, int b){ a += b; printf("In func, a = %d b = %d\n", a, b);}

int main(void){ int x = 5, y = 7; func(x, y); printf("In main, x = %d y = %d\n", x, y); return 0;}The output of the program is:

In func, a = 12 b = 7In main, x = 5 y = 7

Page 9: Classes function overloading

Call By Reference

There are two instances where a variable is passed by reference:

When you modify the value of the passed variable locally and also the value of the variable in the calling function as well.

To avoid making a copy of the variable for efficiency reasons.Passing by by reference refers to a method of passing the address of an argument in the calling function to a corresponding parameter in the called function.

In C, the corresponding parameter in the called function must be declared as a pointer type. In C++, the corresponding parameter can be declared as any reference type, not just a pointer type.

In this way, the value of the argument in the calling function can be modified by the called function.

Page 10: Classes function overloading

Sample ProgramThe following example shows how arguments are passed by reference. In C++, the reference parameters are initialized with the actual arguments when the function is called. In C, the pointer parameters are initialized with pointer values when the function is called.

#include <stdio.h>

void swapnum(int &i, int &j) { int temp = i; i = j; j = temp;}

int main(void) { int a = 10; int b = 20;

swapnum(a, b); printf("A is %d and B is %d\n", a, b); return 0;}

Page 11: Classes function overloading

Call by Value vs Call by Reference The process of calling

function by actually sending or passing the copies of data.

At most one value at a time can be returned to the calling function with an explicit return statement.

Here formal parameters are normal variable names that can receive actual parameters/argument value’s copy.

The process of calling function using pointers to pass the address of variables .

Multiple values can be returned to calling function and explicit return statement is not required .

Here formal parameters are pointer variables that can receive actual parameter or arguments as address of variables .

Page 12: Classes function overloading

Calling a Function using a Pointer

In C++ you call a function using a function pointer by explicitly dereferencing it using the * operator. Alternatively you may also just use the function

pointer's instead of the funtion's name. In C++ the two operators .* resp. ->* are used together with an instance of a class in order to call one of their (non-static) member functions. If the call takes

place within another member function you may use the this-pointer.

Page 13: Classes function overloading

Calling a function using a pointer

EXAMPLE:-main(){TMyClass instance1;int result3 = (instance1.*pt2Member)(12, 'a', 'b'); // C++int result4 = (*this.*pt2Member)(12, 'a', 'b'); // C++ if this-pointer can be used

TMyClass* instance2 = new TMyClass;int result4 = (instance2->*pt2Member)(12, 'a', 'b'); // C++, instance2 is a pointer delete instance2;return 0;}

Page 14: Classes function overloading

Pass Object As An Argument

Like any other data type,an object may be used as a function argument.This can be done in two

ways:

-> A copy of the entire object is passed to the function

-> Only address of the object is transferred to the function

The pass by referrence method is more efficient since it requires to pass only the address of the

object and not the entire object

Page 15: Classes function overloading

Sample Program/*C++ PROGRAM TO PASS OBJECT AS AN ARGUMEMT. The program Adds the two heights given in feet and inches. */

#include< iostream.h>#include< conio.h>

class height{int feet,inches;public:void getht(int f,int i){feet=f;inches=i;}void putheight(){cout< < "\nHeight is:"< < feet< < "feet\t"< < inches< < "inches"< < endl;}

Page 16: Classes function overloading

void sum(height a,height b){height n;n.feet = a.feet + b.feet;n.inches = a.inches + b.inches;if(n.inches ==12){n.feet++;n.inches = n.inches -12;}cout< < endl< < "Height is "< < n.feet< < " feet and "< < n.inches< < endl;}};void main(){height h,d,a;clrscr();h.getht(6,5);a.getht(2,7);h.putheight();a.putheight();d.sum(h,a);getch();}

Page 17: Classes function overloading