C++ function overloading

C++ function overloading

overloading in function refers to the use of the same thing for different purpose. C++ language also permits overloading of functions. This means that we can use the same functions names to create functions that performs a variety of different tasks. This is known as function polymorphism in Object oriented programming.

using the concept of function overloading, we can design a family of function of functions with one function name but with different argument (parameters) list. The function would perform different operations depending on the argument list. The function would perform different operations depending on the argument(parameters) list in the function call. The correct function to be invoked is determined by checking the number of agruments and type of the arguments but not on the function type.

for example an overloaded add() function handles different types of data as shown below

// declarations
int add(int a, int b); //prototype 1
int add(int a, int b, int c); // prototype 2
double add(double x, double y) // prototype 3
double add(int p,double q); // prototype 4
double add(double p, int q); // prototype 5
// FUNCTION calls
std::cout<<add(7,10); // for uses prototype 1
std::cout<<add(67,34,23); // for uses prototype 2
std::cout<<add(34.5,23.6); // for uses prototype 3
std::cout<<add(12,34.7); // for uses prototype 4
std::cout<<add(34.7,12); // for uses prototype 5

A function call first matches the prototype having the same number and type of arguments and then calls the appropriate function for execution. A best match must be unique. The function selection involves the following steps

value=amount(5000,7); //so here one arguments missing

passes the value of 5000 to principal variable and 7 to period variable and then lets the function use default value of 0.15 for rate variable. The call

1. The compiler first tries to find an exact match in which the types of actual arguments are the same, and use that function.
2. if an exact match is not found, the compiler uses the integral promotions to the actual arguments such as

char to int
float to double
to find match

3. When either of them fails, the compiler tries to use the build in conversions(implicit assignment conversions) to the actual arguments and then uses the function whose match is unique. if the conversion is possible to have multiple matches, then the compiler will generate an error message. suppose we use the following two functions
long square(long n) double square(double x)

A function call such as square(12);

will cause an error because int argument can be converted to either long or double, thereby creating an ambiguous situation as to which version of square() should be used.

4. if all of the steps fail, then the compiler will try the user defined conversion in combination with integral promotions and built in conversion to find a unique match user defined conversions are often used in handling class objects.

example of function overloading

// function volume() is overloaded three times

#include <iostream>
using namespace std;
// declarations (prototypes)
int volume(int);
double volume(double, int);
long volume(long,int,int);
int main()
{
std::cout<<volume(10)<<"\n";
std::cout<<volume(2.5,8)<<"\n";
std::cout<<volume(100L,75,15)<<"\n";
return 0;
}
// Function definitions
int volume(int s) //cube
{
return (s*s*s);
}
double volume(double r, int h) // cylinder
{
return (3.14519*r*r*h);
}
long volume(long l, int b, int h) // rectangular box
{
return (l*b*h);
}

The output is:
1000
157.26
112500

overloading of the function should be done with caution. We should not overloaded unrelated functions and should reserve function overloading for function that perform closely related tasks. sometimes , the default arguments may be used instead of overloading. This may reduce the number of functions to be defined.

overloaded functions are extensively used for handling class objects. They will be illustrated later when the classes are discussed in next pages

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise