Coupon Accepted Successfully!


Function Overloading

It is possible in C++ to use the same function name for number of times and for different intentions. Defining multiple functions with same name is known as function overloading or function polymorphism. Polymorphism means one function having many forms. The overloaded function must be different in their argument list and with different data types. The following are examples of overloaded functions. All the functions defined should be equivalent to their prototypes.


7.19 Write a program to calculate square of an integer and float number. Define function sqr(). Use function-overloading concept.


Explanation: In the above function, sqr() is overloaded for integer and float. In the first call of the function sqr(), an integer 15 is passed. The compiler executes integer version of the function and returns result 225. In the second call a float value 2.5 is passed to function sqr(). In this call, the compiler executed the float version of the function and returns the result 6.25. The selection of which function to execute is decided at run time by the compiler according to the data type of variable passed.

7.20 Write a program to find the area of rectangle, triangle and surface area of sphere. Use function overloading.


Explanation: In the above program, function calcarea() is overloaded. It is used for finding the area of a rectangle, triangle, and sphere. Explanation is same as detailed in previous problem.

Test Your Skills Now!
Take a Quiz now
Reviewer Name