0% found this document useful (0 votes)
64 views4 pages

Function Overloading: Void Foo (Int I, Char A) Void Boo (Int J, Char B)

Function overloading allows defining multiple functions with the same name but different parameters. In C++, functions can be overloaded based on the number, type, or order of parameters. The example demonstrates overloading the calc function to either calculate the square of a number or the product of two numbers based on the parameters passed. Operator overloading similarly allows defining new behaviors for operators like + based on the operands. It is implemented by defining corresponding member or friend functions. The example overloads the + operator to add the values of two user-defined type objects. Operator overloading enhances reusability but needs to be used carefully for efficiency and readability.

Uploaded by

Siddharth Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views4 pages

Function Overloading: Void Foo (Int I, Char A) Void Boo (Int J, Char B)

Function overloading allows defining multiple functions with the same name but different parameters. In C++, functions can be overloaded based on the number, type, or order of parameters. The example demonstrates overloading the calc function to either calculate the square of a number or the product of two numbers based on the parameters passed. Operator overloading similarly allows defining new behaviors for operators like + based on the operands. It is implemented by defining corresponding member or friend functions. The example overloads the + operator to add the values of two user-defined type objects. Operator overloading enhances reusability but needs to be used carefully for efficiency and readability.

Uploaded by

Siddharth Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Function overloading

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.

a) The number of parameters 


b) The data type of parameters
c) The order of appearance

These three together are referred to as the function signature. 

For example if we have two functions :

void foo(int i,char a);


void boo(int j,char b);

Their signature is the same (int ,char) but a function 

void moo(int i,int j) ; has a signature (int, int) which is different.

#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);
}

I will explain what we did in the function overloading example.

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. 

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


Square of a given number: 25
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 :
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.

C++ Operator Overloading


C++ Operator Overloading
Operator overloading is a very important aspect of object-oriented programming. Binary operators can be overloaded
in a similar manner as unary operators. In this C++ tutorial, you will learn about Binary Operating Overloading
explained along with syntax and example.

Operator Overloading – Binary Operators


Binary operators, when overloaded, are given new functionality. The function defined for binary operator overloading,
as with unary operator overloading, can be member function or friend function.

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.

Binary operator overloading example:

#include <iostream.h>
class Exforsys
{
private:
int x;
int y;

public:
Exforsys()                    //Constructor
{ x=0; y=0; }

void getvalue( )             //Member Function for Inputting Values


{
cout << “\n Enter value for x: “; 
cin >> x;
cout << “\n Enter value for y: “; 
cin>> y;
}

void displayvalue( )          //Member Function for Outputting Values


{
cout <<”value of x is: “ << x <<”; value of y is: “<<y
}
Exforsys operator +(Exforsys);
};

Exforsys Exforsys :: operator + (Exforsys e2)


//Binary operator overloading for + operator defined
{
int x1 = x+ e2.x;
int y1 = y+e2.y;
return Exforsys(x1,y1);
}

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();
}

The output of the above program is:

Enter value for Object e1:


Enter value for x: 10
Enter value for y: 20
Enter value for Object e2:
Enter value for x: 30
Enter value for y: 40
Value of e1 is: value of x is: 10; value of y is: 20
Value of e2 is: value of x is: 30; value of y is: 40
Value of e3 is: value of x is: 40; value of y is: 60

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.

The important aspect is the statement:

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.

You might also like