Function overloading in C Ms Rachana T Nemade

  • Slides: 6
Download presentation
Function overloading in C++ -Ms Rachana T Nemade -Mrs Sangita Nemade

Function overloading in C++ -Ms Rachana T Nemade -Mrs Sangita Nemade

�To specify more than one definition for a function name in the same scope,

�To specify more than one definition for a function name in the same scope, is called function overloading. �To specify more than one definition for an operator in the same scope, is called operator overloading. Function overloading and Operator overloading

�An overloaded declaration is a declaration that had been declared with the same name

�An overloaded declaration is a declaration that had been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation). Function overloading and Operator overloading

Function Overloading Example /*Calling overloaded function test() with different arguments. */ #include <iostream> using

Function Overloading Example /*Calling overloaded function test() with different arguments. */ #include <iostream> using namespace std; void test(int); void test(float); void test(int, float); int main() { int a = 5; float b = 5. 5; test(a); test(b); test(a , b); return 0; }

void test(int var) { cout<<"Integer number: "<<var<<endl; } void test(float var){ cout<<"Float number: "<<var<<endl;

void test(int var) { cout<<"Integer number: "<<var<<endl; } void test(float var){ cout<<"Float number: "<<var<<endl; } void test(int var 1, float var 2) { cout<<"Integer number: "<<var 1; cout<<" And float number: "<<var 2; } Function Overloading Example

�In above example, �function test() is called with integer argument at first. �Then, functiontest()

�In above example, �function test() is called with integer argument at first. �Then, functiontest() is called with floating point argument �and finally it is called using two arguments of type int and float. �Although the return type of all these functions is same, that is, void, it's not mandatory to have same return type for all overloaded functions. Function Overloading Example