Course Object Oriented Programming

Overloading Unary Operator

Course's Page 10 min Text by
User Image
Allan Garcia

Let us now look at the overload of Unary Operators.

Unary Operators

Operators ++, --, ! and ~ only use one operand. Operators +, -, * and & can use only one operand.

As we saw in last class, operators who only use one operand are known as unary operators. E.g: i++, i--, !a and ~b.

The latter, which can use one or two operands, are the binary or unary operators. E.g: +5, -5, *p and &x.

How to define a Unary Operator

Thus, to define a new unary operator for the class, we use the syntax:

    <return> <operator> () {
        // Code
    }