Pages

Friday, July 15, 2011

Polymorhpism - Function Overloading

As mentioned in the previous post, polymorphism is the ability of an object to behave differently in different conditions. To practically implement the concept of polymorphism in C++ one of two methods is Function Overloading.


Function Overloading
To put it simply , function overloading is declaring two or more functions with the same. The idea behind using the same name for multiple methods is to save the user from the trouble of remembering different names for different methods. For example, if there is a method that adds two integers the most suitable name will be "add" or "sum" or any other logical name. Imagine another method that adds two float values, or another one that adds three integers. One way of naming all these methods will be assigning different name to all the methods like "add1","add2", "add3", but it will make the whole process very confusing. Since all the methods are doing similar tasks(addition) the concept of function overloading will make the task easy for us by allowing  same name for all these functions. 

Calling overloaded functions

Since all the functions have same name, how will the compiler decide which method to call. The answer is - by looking at the arguments. All the overloaded functions must be 
a) In The Same Class : You can not declare two functions with the same name in a parent and a child class and call it "Overloaded".
b) Their arguments must be different : The methods must differ in either number, datatype or order.

Example
In general functions are overloaded when :
1. Functions differ in function signature. 
2. Return type of the functions is the same.
Here s a basic example of function overloading
  1. #include
  2.  
  3.  
  4. class sample {
  5. public:
  6.  void calc(int num1)
  7.  
  8. {
  9. cout<<"Square of a given number: " <<num1*num1 <<endl;
  10. }
  11.  
  12.  void calc(int num1, int num2 )
  13.  
  14. {
  15. cout<<"Product of two whole numbers: " <<num1*num2 <<endl;
  16. }
  17. };
  18.  
  19.  
  20. int main()  
  21. {
  22.     sample s;
  23.     s.calc(2);
  24.     s.calc(2,3);
  25. }
First the overloaded function in this example is calc. If you have noticed we have in our sample class two functions with the name calc. The fist one takes one integer number as a parameter and prints the square of the number. The second calc function takes two integer numbers as parameters, multiplies the numbers and prints the product. This is all we need for making a successful overloading of a function.
a) we have two functions with the same name : calc
b) we have different signatures : (int) , (int, int)
c) return type is the same : void

The result of the execution looks like this
  1. Square of a given number: 25
  2. Product of two whole numbers: 42
The result demonstrates the overloading concept. Based on the arguments we use when we call the calc function in our code :
  1. s.calc(5);
  2. s.calc(6,7);
The compiler decides witch function to use at the moment we call the function. Remember that argument name of the methods and the return value does not matter in the whole process.

0 comments:

Post a Comment