What is Unary Operator in C++?
To perform mathematical and logical operations on numerical values, C++ has a wide variety of operators. One such extensively used operator is the unary operator. Unary operators are the operators that operate on a single operand to produce a specific value. Unary operators in C++ include logical NOT, dereferencing operator, bitwise NOT, increment operator, decrement operator, address of operator, size of the operator, and the unary plus and minus operators.
Types of Unary Operators
The C++ programming language supports the following unary operators:
1. Unary Plus
The unary plus operator is represented using the symbol +, which does not change to the operand value. It always represents the operands' values. Syntax
Example Here is a program to demonstrate the use of the Unary plus + operator.
Output
2. Unary Minus
The unary minus operator is represented by using the symbol -. It changes the sign of its argument. A positive number turns a negative number, and a negative number turns a positive one.
Syntax
Example Here is a program to demonstrate the use of the Unary minus - operator.
Output
3. Increment Operator
The symbol ++ denotes the increment operator. It always increases the value by 1. It can be used both as a pre-increment and a post-increment operator.
- The pre-increment operator is represented as ++x. In this operator, the value of the operand will be altered (increased by 1) before it is used.
- The post-increment operator is represented as x++. In this operator, the value of the operand will be altered (increased by 1) after it is used.
Syntax
Example Here is a program to demonstrate the use of the increment operator as the pre-increment and the post-increment operator.
Output
4. Decrement Operator
The symbol -- denotes the decrement operator. It always decreases the value by 1. It can be used both as a pre-decrement and a post-decrement operator.
- The pre-decrement operator is represented as --x. In this operator, the value of the operand will be altered (decreased by 1) before it is used.
- The post-decrement operator is represented as x--. In this operator, the value of the operand will be altered (decreased by 1) after it is used.
Syntax
Example Here is a program to demonstrate using the decrement operator as the pre-decrement and the post-decrement operator.
Output
5. Size of the Operator
This unary operator is used to return the size of the operand in bytes. For example, the int data type in C++ has a 4 bytes size. The sizeof() operator will return this value. The sizeof() operator is used as a function in the program. The variable type and the amount of memory required to store the value are shown in the table below. The values shown here may differ from one compiler to another. GCC 32 bit was used in the following values.
Sr. No. | Data Type | Size (in Bytes) |
---|---|---|
1. | int | 4 Bytes |
2. | short int | 2 Bytes |
3. | long int | 4 Bytes |
4. | long long int | 8 Bytes |
5. | char | 1 Bytes |
6. | float | 4 Bytes |
7. | double | 8 Bytes |
8. | long double | 12 Bytes |
Syntax
Example Here is a program to demonstrate the use of the sizeof() operator.
Output
6. Address of the operator
This operator is used to return the memory address of a variable. The addresses that the address-of operator returns are referred to as pointers since they "point" to the variable's memory location.
Syntax
Example Here is a program to demonstrate the use of the address-of & operator.
Output
7. Dereferencing Operator
The dereferencing operator, also known as an indirection operator, operates on a pointer variable. It is denoted with an asterisk, *. It returns the location value or l-value in memory pointed to by the variable's value.
Syntax
Example Here is a program to demonstrate the use of the dereferencing * operator.
Output
8. Logical NOT
The logical NOT operator is denoted with the symbol !. It is also known as a Logical Negation Operator, which is used to reverse the logical state of its operand. The logical NOT operator will make a condition false if it is true and vice-versa.
Syntax
Example Here is a program to demonstrate the use of the logical NOT ! operator.
Output
9. Bitwise NOT
The bitwise NOT operator is also known as One's Complement Operator. It is denoted by the symbol ~. It takes one operand and inverts all bits of it.
Syntax
Example Here is a program to demonstrate the use of the bitwise NOT ~ operator.
Output
What is Unary Operator Overloading in C++
Unary Operator Overloading means overloading the unary operators. Unary operators are those operators which operate on a single operand to produce a new value. Unary operator overloading works only with one class object. We can overload a unary operator with either a member function with no arguments or a nonmember function (friend function) with one argument. The syntax for the operator function when the operator function is a member function:
The syntax for the operator function when the operator function is a nonmember function:
C++ Program for Unary Operator Overloading
Here is a C++ program to show how Increment ++ and decrement -- operators can be overloaded for prefix usage.
Output
Explanation In the above C++ program, when we use ++comp1 to increase the complex number, the void operator ++ () is called. This function increases the x and y attribute for the object comp1 by 1. When we use --comp1 to decrease the complex number, the void operator -- () is called. This function reduces the x and y attribute for the object comp1 by 1. The above example works only when the ++ operator is used as a prefix.
Note: In the above C++ program, comp2 = ++comp1 or comp2=--comp1 will not work, because operator ++() and operator --() do not return any value.
Learn More
Conclusion
- Unary operators are the operators that operate on a single operand to produce a new value.
- The sizeof() unary operator returns the operand size in bytes. It is used as a function in the program.
- The logical NOT operator is denoted with the symbol !. It is used to reverse the given condition. For example, if a condition is true, then the Logical NOT operator will make it false.
- We can overload a unary operator with either a member function with no arguments or a nonmember function with one argument.
- Unary Operator Overloading means overloading the unary operators, which operate on a single operand to produce a new value.