Function Overloading: Void Foo (Int I, Char A) Void Boo (Int J, Char B)
Function Overloading: Void Foo (Int I, Char A) Void Boo (Int J, Char B)
A function is overloaded when same name is given to different function. However, the two functions with the same name will differ at
least in one of the following.
#include<iostream.h>
#include<conio.h>
class arith {
public:
void calc(int num1)
{
cout<<"Square of a given number: " <<num1*num1 <<endl;
}
void calc(int num1, int num2 )
{
cout<<"Product of two whole numbers: " <<num1*num2 <<endl;
}
};
int main() //begin of main function
{
arith a;
a.calc(5);
a.calc(6,7);
}
First the overloaded function in this example is calc. If you have noticed we have in our arith 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.
The result demonstrates the overloading concept. Based on the arguments we use when we call the calc function in our code :
a.calc(5);
a.calc(6,7);
The compiler decides witch function to use at the moment we call the function.
Operator overloading
Operator overloading is a very important feature of Object Oriented Programming. Curious to know why!!? It is
because by using this facility programmer would be able to create new definitions to existing operators. In fact in
other words a single operator can take up several functions as desired by programmers depending on the argument
taken by the operator by using the operator overloading facility.
The difference is in the number of arguments used by the function. In the case of binary operator overloading, when
the function is a member function then the number of arguments used by the operator member function is one (see
below example). When the function defined for the binary operator overloading is a friend function, then it uses two
arguments.
Binary operator overloading, as in unary operator overloading, is performed using a keyword operator.
#include <iostream.h>
class Exforsys
{
private:
int x;
int y;
public:
Exforsys() //Constructor
{ x=0; y=0; }
void main( )
{
Exforsys e1,e2,e3; //Objects e1, e2, e3 created
cout<<\n”Enter value for Object e1:”;
e1.getvalue( );
cout<<\n”Enter value for Object e2:”;
e2.getvalue( );
e3= e1+ e2; //Binary Overloaded operator used
cout<< “\nValue of e1 is:”<<e1.displayvalue();
cout<< “\nValue of e2 is:”<<e2.displayvalue();
cout<< “\nValue of e3 is:”<<e3.displayvalue();
}
In the above example, the class Exforsys has created three objects e1, e2, e3. The values are entered for objects e1
and e2. The binary operator overloading for the operator ‘+’ is declared as a member function inside the class
Exforsys. The definition is performed outside the class Exforsys by using the scope resolution operator and the
keyword operator.
e3= e1 + e2;
The binary overloaded operator ‘+’ is used. In this statement, the argument on the left side of the operator ‘+’, e1, is
the object of the class Exforsys in which the binary overloaded operator ‘+’ is a member function. The right side of the
operator ‘+’ is e2. This is passed as an argument to the operator ‘+’ . Since the object e2 is passed as argument to
the operator’+’ inside the function defined for binary operator overloading, the values are accessed as e2.x and e2.y.
This is added with e1.x and e1.y, which are accessed directly as x and y. The return value is of type class Exforsys as
defined by the above example.
There are important things to consider in operator overloading with C++ programming language. Operator overloading
adds new functionality to its existing operators. The programmer must add proper comments concerning the new
functionality of the overloaded operator. The program will be efficient and readable only if operator overloading is used
only when necessary.