Broad Network


Unary Operators in C++

C++ Operators – Part 5

Forward: In this part of the series we look at Unary Operators in C++.

By: Chrysanthus Date Published: 23 Aug 2012

Introduction

This is part 5 of my series, C++ Operators. A C++ unary operator needs only one operand to work with, normally on its left. In this part of the series we look at Unary Operators in C++. The direction of operation is normally from right to left.; the operand is on the left of the operator.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The Logical Negation Operator !
This is the NOT operator. We have seen it before. It takes a Boolean operand. If the operand is false, it will return true; if it is true, it will return false. In C++ Boolean true can be represented by 1 and Boolean false is represented by 0.

The if-block will be executed in the following code:

#include <iostream>
using namespace std;

int main()
    {
        if (!(0))
            {
                cout << "I am tall";
            }

        return 0;
    }

The if-block is executed, if the condition is true. !(false) gives true.

A practical example for the above code is:

#include <iostream>
using namespace std;

int main()
    {
        //Let tall means 20 and short means 10
        int me = 20;

        if (!(me == 10))
            {
                cout << "I am tall";
            }

        return 0;
    }

The – Operator
The – operator can have an arithmetic or enumerated type operand. In this article I only talk about the arithmetic type operand. If the value of the operand is a positive number, it changes it to a negative number. If it is a negative number, it changes it to a positive number. This symbol can behave as two operators; negation and addition. In this part of the series, we see it as a negation operator. The following example illustrates this:

#include <iostream>
using namespace std;

    int main()
        {
            signed int int1 = +5;
            signed int int2 = -6;

            signed int intA = -int1;
            signed int intB = -int2;    

            cout << intA<<"\n";
            cout << intB;

            return 0;
     }

If you try the above code, the value of intA will be –5 and the value of intB will be +6 (same as 6).

The + Operator
The + operator does the opposite of the – operator. The + operator can also have a pointer type operand. However, in this series I will only talk about the arithmetic operand. Note: like in mathematics, the + operator does not change the sign of a number, when it precedes the signed number. The following code illustrates the use of the + operator (you do not always have to use it):

#include <iostream>
using namespace std;

    int main()
        {
            signed int int1 = +5;
            signed int int2 = -6;

            signed int intA = +int1;
            signed int intB = +int2;    

            cout << intA<<"\n";
            cout << intB;

            return 0;
     }

The signs of int1 and int2 are not changed.

The ~ Operator
The ~ operator is an example of what is known as a bitwise operator. I intend to produce a whole series on bitwise operators. I will not address such operators in this series. You do not need bitwise operators for ordinary programming.

The increment Operator
The Increment Operator is, ++. The operand has to be a number (it can also be a pointer but I will not address that). When it is placed in front (prefix) of the operand, it behaves in one way. When it is placed after (postfix) the operand it behaves in another way.

Prefix: When it is prefix, it adds 1 to the operand and returns the incremented operand value. Read and try the following code:

#include <iostream>
using namespace std;

int main()
    {
        int id1 = 10;
        
        int id2 = ++id1;
        
        cout << id2;
        
        return 0;
    }

In the code, initially, 10 is assigned to the object of id1. Then we have a new statement. In the statement you have a new identifier, id2, the assignment operator and then “++id1”. What interest us here is “++id1”, where the increment operator is in front of the identifier. The value the increment operator returns is assigned to the object of id2. If you have tried the code, you would have noticed that the value of id2 is 11. This means, if used prefix, it increments the operand and then returns the incremented content of the operand object.

Postfix: When it is postfix, it returns the operand value before adding 1 to it. The returned value is the original value of the operand. The increased value is the new value of the operand, which is not returned. Read and try the following code.

#include <iostream>
using namespace std;

int main()
    {
        int id1 = 10;
        
        int id2 = id1++;
        
        cout << id2<<"\n";
        cout << id1;
        
        return 0;
    }

If you have tried the above code, you would have noticed that the value for id2 is 10 and the final value for id1 is 11, confirming that the incrementing took place after the value was returned. When it is placed postfix, the direction of operation is left to right and not right to left. Also remember that when it is placed postfix, the value of the operand is returned before it is incremented.

The new and delete Operator
The new operator is the reserved word, new, and the delete operator is the reserved word, delete. The new and delete operators work with dynamic objects. If you do not understand dynamic objects, then read the article I wrote titled, “Dynamic Objects in C++”. To reach the article, type the title of the article and my name Chrys in the Search box of this page and click Search.

The following statement shows how you use the new operator to create a dynamic object of type int:

    int *myIntPtr = new int;

The new operator returns a pointer to the dynamic object. You can then use the pointer to access the object. There are two other ways of using the new operator to create a dynamic object, but I will not go into that in this series. You can read the article I wrote for more info on that.

The following statement shows how you use the delete operator to delete a dynamic object (it does not matter the type of dynamic object):

        delete myIntPtr;

You begin with the operator and then you follow that with the pointer object identifier. There is another way of using the delete operator, but I will not go into that in this series. You can read the article I wrote for more info on that.

Note: The new and delete reserved words ant not built-in functions, they are built-in operators.

The sizeof Operator
The reserved word, sizeof is not a built-in function, it is a built-in operator. Its operand is either an object type (e.g. char, int, float) or a particular object (identifier of the object). When the operand is an object type the operand goes into parentheses; when it is an object, the parentheses are optional. This operator gives the size of the object type or object in bytes.

For all computers the following expression returns 1, which is the size in bytes of any char:

    sizeof(char);

The size of the other fundamental object types vary with computers. The following expression will return the size of an int in your computer.

    sizeof(int)

The following code segment displays the size of the int object, myInt:

        int myInt = 553;
        cout << sizeof myInt;

Note: the size of an int object is the size of the int type; the size of a float object is the size of the float type; the size of a char object is the size of the char type, and so on. Apart from char, the other sizes depend on your computer.

You can also use the sizeof operator to know the size of a particular array. The size of an array is the number of array elements times the size of each element. The following code illustrates this:

#include <iostream>
using namespace std;

int main()
    {
        float myFlt[15];
        cout << sizeof myFlt;
        
        return 0;
    }

In my computer, the size of a float is 4, so the output of the above code is 60, determined as 15 times 4, since 15 is the number of elements.

Note that just the name of the array has been used as the operand of sizeof without the [] brackets of the array.

The * and & Operators
The * and & operators are used with pointers. * means the value in the pointed object pointed to by a pointer object. & means the address of an object. The following code illustrates these:

#include <iostream>
using namespace std;

int main()
    {
        float hisFloat;
        float *myPointer = &hisFloat;

        *myPointer = 23.5;

        cout << *myPointer;

        return 0;
    }

In the third statement of main, the operand of * is myPointer which is the identifier of an object (pointer object), which has the address of the object, hisFloat. In the second statement the operand of & is hisFloat, which is the identifier of an object (pointed object). The expression created by & returns the address of the object of its operand (hisFloat). The third statement gives hisFloat, the value, 23.5.

Note that when a pointer is created by initialization, * does not mean value of pointed object; it means value (address) in pointer object. Pointer object and pointed object are two different things (two different objects). An object is a region in memory.

The unary operators we have not seen are known as the Type Casting Operators. We shall see that in the next part of the series.

The unary operators are: * & + - ! ˜ ++ -- * & new delete sizeof and type casting operators.

Let us take a break here and continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message