Javatpoint Logo
Javatpoint Logo

Unary Operator Overloading in C++

It is polymorphism when we overload an operator to carry out the same operation on objects of the same class. Simple unary operators cannot be used with class objects since they do not comprehend the behavior of member variables and will result in compilation errors. We use operator overloading for user-defined data type (object of a class) operations to give operators a user-defined meaning.

We may use unary operator overloading in C++ by defining a new operator function in the class itself or by using the global friend function made for the operator function.

What is a Unary Operator in C++?

C++ comes with a large range of operators that can be used to perform mathematical and logical operations on numerical numbers. One such widely used operator is the unary operator. Unary Operators are employed in operations with a single operand. Unlike binary operators, unary operators do not need two operands to calculate the result.

The unary operator is used to determine the new value of a single operand or variable. When using unary operators, the operand can be in either the prefix or postfix position. Unary operators have equal precedence, right-to-left associativity, and a variety of forms.

Syntax:

The following is the syntax for C++'s unary operator overloading:

Here are a few examples of unary operators:

  • Decrement operator (--),
  • Increment operator (++),
  • Logical not operator (!),
  • Unary minus operator (-),
  • Address of (&), etc.

Let's use an example to clarify unary operator overloading in C++.

I). Unary Operator Overloading for User-Defined Classes:

Output:

Original complex number: 3 + 4i
Negated complex number: -3 + -4i

II. Overloading the class Member function for the unary minus (-) operator

Let's take an example to understand how to define the class Member function for the unary minus (-) operator in C++:

Output:

num1: Value: 7
num2 (after unary minus): Value: -7

III. Overloading the global friend function's unary minus (-) operator

Let's take an example to understand how to define the global friend function's unary minus (-) operator in C++:

Output:

c1 = -5 - i4
c2 = 5 + i4

Explanation:

  1. In this example, we created a friend function in the Complex with a globally overloaded minus (-) unary operator
  2. A friend function in C++ is a unique function that is not a member of a class but has access to its protected and private data.
  3. A global friend function that can access the real and image variables has been given that has an overloaded definition for the global minus (-) operator outside of the Complex class. After using the c2 object and the unary minus(-) operator in the main() function, it is called.

Conclusion:

  • In C++, unary operators are used for operations with a single operand or variable.
  • Here are a few instances of unary operators: Unary minus operator (-), Logical not operator (! ), Increment operator (++), Decrement operator (- -),
  • In C++, polymorphism (known as unary operator overloading) is used to make an operator more capable of carrying out similar operations on objects belonging to a class.
  • We use operator overloading to give operators a user-defined meaning for user-defined data type (object of a class) operations.
  • We may use unary operator overload by defining a new operator function in the class itself or by utilizing the global friend function made for the operator function.

Next TopicUnique_ptr in C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA