Introduction Presentation: void FunctionOverloading ( ); Presented to: prof. Mam Asma Basharat. Presented by: Zain Javaid (071-bscs08)
Function Overloading C++ supports writing more than one function with the different signature. This could include: – – – – –
Same function name Same return type of functions Different argument list Different argument types Different order of arguments
The advantage is that the same apparent function can be called to perform similar but different tasks. The following will show an example of this.
Signature of function The first step in understanding function overloading is to understand what constitutes a function signature. int foo(int x, double y) The signature of the above function is: foo(int, double) Now, consider the following two functions: int foo(int x) double foo(int x) The above functions are not considered to be different, their signatures are the same because the return value of a function is not part of its signature, this will cause an error when compiling the code: foo(int x) foo(int x) Finally, consider these functions: int bar(int x, double y) int bar(int z, double q) The above functions are not considered to be different, changing the names of the argument variables does not alter the signature: bar(int, double) bar(int, double)
Same function name To perform function overloading the name of functions must be same – void sum( int x, int y); – void add( int x, int y);
The above functions are not considered to be overloaded, because in overloading two or more function with same name are needed.
Same return type of functions To perform function overloading the return type of functions must be same – int bar(int x) – double bar(int x) The above functions are not considered to be different, their signatures are the same because the return value of a function is not part of its signature, this will cause an error when compiling the code: – bar(int x) – bar(int x)
Different argument list To perform function overloading the argument list must be different – int bar(int z) – int bar(int x, int y)
Although the functions above have the same name, they are treated as different functions by the compiler because their signatures are different: – bar(int) – bar(int, int)
Different argument types To perform function overloading the argument types must be different – int bar( int x, float y) – int bar( int x, int y)
Although the functions above have the same name, they are treated as different functions by the compiler because their signatures are different: – bar(int, float) – bar(int, int)
Different order of arguments To perform function overloading the order of argument must be different – int bar( int x, float y) – int bar( float x, int y)
Although the functions above have the same name, they are treated as different functions by the compiler because their signatures are different: – bar(int, float) – bar(float, int)
C++ - Sample code for function overloading void AddAndDisplay(int x, int y) { cout<<" C++ Tutorial - Integer result: "<<(x+y); } void AddAndDisplay(double x, double y) { cout<< " C++ Tutorial - Double result: "<<(x+y); } void AddAndDisplay(float x, float y) { cout<< " C++ Tutorial - float result: "<<(x+y); }
C++ - Sample code for function overloading Here are example Function overloading class FuncOver { public: // Constructors can also be overloaded. FuncOver(); // Overloaded constructor. FuncOver(int i); // Functions example. int sum(int a int b); int sum(float a float b); };