Overloading Unary Operators in C++

By
Last updated:

Let’s start off by overloading a unary operators. unary operators act on only one operand. (An operand is simply a variable acted on by an operator.) Examples of unary operators are the increment and decrement operators ++ and –, and the unary minus, as in -33.

“Objects and Classes,” we created a class Counter to keep track of a count. Objects of that class were incremented by calling a member function:

c1.inc_count();

That did the job, but the listing would have been more readable if we could have used the increment operator ++ instead:

++c1;

All dyed in the wool C++ (and C) programmers would guess immediately that this expression increments c1.

Let’s rewrite COUNTER to make this possible. Here’s the listing for COUNTPP1:

// countpp1.cpp
// increment counter variable with ++ operator
#include  <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Counter
{
private:
unsigned int count; //count
public:
Counter() : count(0) //constructor
{ }
unsigned int get_count() //return count
{ return count; }
void operator ++ () //increment (prefix)
{
++count;
}
};
////////////////////////////////////////////////////////////////
int main()
{
Counter c1, c2; //define and initialize
cout << “\nc1=” << c1.get_count(); //display
cout << “\nc2=” << c2.get_count();
++c1; //increment c1
++c2; //increment c2
++c2; //increment c2
cout << “\nc1=” << c1.get_count(); //display again
cout << “\nc2=” << c2.get_count() << endl;
return 0;
}

In this program we create two objects of class Counter: c1 and c2. The counts in the objects are displayed; they are initially 0. Then, using the overloading unary operators ++ operator, we increment c1 once and c2 twice, and display the resulting values. Here’s the program’s output:

c1=0 ←——– counts are initially 0
c2=0
c1=1 ←——- incremented once
c2=2 ←——- incremented twice

The statements responsible for these operations are
++c1;
++c2;
++c2;

The ++ operator is applied once to c1 and twice to c2. We use prefix notation in this example; we’ll explore postfix later.

The operator Keyword

How do we teach a normal C++ operator to act on a user defined operand? The keyword operator is used to overload the ++ operator in this declarator:

void operator ++ ()

The return type (void in this case) comes first, followed by the keyword operator, followed by the operator itself (++), and finally the argument list enclosed in parentheses (which are empty here). This declarator syntax tells the compiler to call this member function whenever the ++ operator is encountered, provided the operand (the variable operated on by the ++) is of type Counter.

“Functions,” that the only way the compiler can distinguish between overloading unary operators functions is by looking at the data types and the number of their arguments. In the same way, the only way it can distinguish between overloaded operators is by looking at the data type of their operands. If the operand is a basic type such as an int, as in ++intvar; then the compiler will use its built-in routine to increment an int. But if the operand is a Counter variable, the compiler will know to use our user written operator++() instead.

Operator Arguments

In main() the ++ operator is applied to a specific object, as in the expression ++c1. Yet operator++() takes no arguments. What does this operator increment? It increments the count data in the object of which it is a member. Since member functions can always access the particular object for which they’ve been invoked, this operator requires no arguments.

Operator Return Values

The operator++() function in the COUNTPP1 program has a subtle defect. You will discover it if you use a statement like this in main(): c1 = ++c2;

The compiler will complain. Why? Because we have defined the ++ operator to have a return type of void in the operator++() function, while in the assignment statement it is being asked to return a variable of type Counter. That is, the compiler is being asked to return whatever value c2 has after being operated on by the ++ operator, and assign this value to c1. So as defined in COUNTPP1, we can’t use ++ to increment Counter objects in assignments; it must always stand alone with its operand. Of course the normal ++ operator, applied to basic data types such as int, would not have this problem.

To make it possible to use our homemade operator++() in assignment expressions, we must provide a way for it to return a value. The next program, COUNTPP2, does just that.

// countpp2.cpp
// increment counter variable with ++ operator, return value
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Counter
{
private:
unsigned int count; //count
public:
Counter() : count(0) //constructor
{ }
unsigned int get_count() //return count
{ return count; }
Counter operator ++ () //increment count
{
++count; //increment count
Counter temp; //make a temporary Counter
temp.count = count; //give it same value as this obj
return temp; //return the copy
}
};
////////////////////////////////////////////////////////////////
int main()
{
Counter c1, c2; //c1=0, c2=0
cout << “\nc1=” << c1.get_count(); //display
cout << “\nc2=” << c2.get_count();
++c1; //c1=1
c2 = ++c1; //c1=2, c2=2
cout << “\nc1=” << c1.get_count(); //display again
cout << “\nc2=” << c2.get_count() << endl;
return 0;
}

Here the operator++() function creates a new object of type Counter, called temp, to use as a return value. It increments the count data in its own object as before, then creates the new temp object and assigns count in the new object the same value as in its own object. Finally, it returns the temp object. This has the desired effect. Expressions like ++c1

now return a value, so they can be used in other expressions, such as c2 = ++c1;

as shown in main(), where the value returned from c1++ is assigned to c2. The output from this program is
c1=0
c2=0
c1=2
c2=2

Read More Topics
Virtual Function in C++
Overloaded function in C++
Arrays structures and unions in C
C++ Programming basics

Santhakumar Raja

Hello The goal of this blog is to keep students informed about developments in the field of education. encourages pupils to improve as writers and readers.

For Feedback - techactive6@gmail.com

Leave a Comment