Binary Operator Overloading

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Overloading Unary Operator:

Let us consider to overload (-) unary operator. In unary operator function, no


arguments should be passed. It works only with one class objects. It is a
overloading of an operator operating on a single operand.

Example:
Assume that class Distance takes two member object i.e. feet and inches, create a
function by which Distance object should decrement the value of feet and inches by 1
(having single operand of Distance Type).

// C++ program to show unary operator overloading


#include <iostream.h>
#include <conio.h>

class Distance
{
public:

// Member Object
int feet, inch;

// Constructor to initialize the object's value


Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}

// Overloading(-) operator to perform decrement


// operation of Distance object
void operator-()
{
feet--;
inch--;
cout << "\nFeet & Inches(Decrement): " << feet << "'" << inch;
}
};

// Driver Code
int main()
{
// Declare and Initialize the constructor
Distance d1(8, 9);

// Use (-) unary operator by single operand


-d1;
return 0;
}

Output:

Feet & Inches(Decrement): 7'8

In the above program, it shows that no argument is passed and no return_type value
is returned, because unary operator works on a single operand. (-) operator change
the functionality to its member function.

Overloading Binary Operator:


In binary operator overloading function, there should be one argument to be
passed. It is overloading of an operator operating on two operands.

Let’s take the same example of class Distance, but this time, add two distance
objects.

// C++ program to show binary operator overloading


#include <iostream.h>
#include <conio.h>

class Distance
{
public:
// Member Object
int feet, inch;
// No Parameter Constructor
Distance()
{
this->feet = 0;
this->inch = 0;
}

// Constructor to initialize the object's value


// Parametrized Constructor
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}

// Overloading (+) operator to perform addition of


// two distance object
Distance operator+(Distance& d2) // Call by reference
{
// Create an object to return
Distance d3;

// Perform addition of feet and inches


d3.feet = this->feet + d2.feet;
d3.inch = this->inch + d2.inch;

// Return the resulting object


return d3;
}
};

// Driver Code
int main()
{
// Declaring and Initializing first object
Distance d1(8, 9);

// Declaring and Initializing second object


Distance d2(10, 2);

// Declaring third object


Distance d3;

// Use overloaded operator


d3 = d1 + d2;

// Display the result


cout << "\nTotal Feet & Inches: " << d3.feet << "'" << d3.inch;
return 0;
}

Output:
Total Feet & Inches: 18'11

Distance operator+(Distance &d2),


return type of function is distance and it uses call by references to pass an
argument.

d3 = d1 + d2;
d1 calls the operator function of its class object and takes d2 as a parameter, by
which operator function return object and the result will reflect in the d3 object.

You might also like