Broad Network


Isolated Operators in C++

C++ Operators – Part 8

Forward: In this part of the series, we look at isolated operators, such as the conditional operator, in C++.

By: Chrysanthus Date Published: 24 Aug 2012

Introduction

This is part 8 of my series, C++ Operators. All the operators we have seen so far exist in groups of at least two operators per group. The assignment operators have the simple assignment operator, the += operator, the -= operator, etc. in one group. By isolated operators, I am referring to operators that do not belong to any group and just exists on their own.  In this part of the series, we look at isolated operators in C++.

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 Conditional Operator
An operator known as the conditional operator is ?:. It is the ? and : signs separated. The syntax of this operator is:

        condition ? return this value if true : return this other value if false

So you have an if-condition. If it is evaluated to true, the value after the ? sign is returned. If the condition is evaluated to false the value after the : sign is returned. The following code illustrates this:

#include <iostream>
using namespace std;

int main()
    {
        int a = 7;
        int b = 8;

        int c = b>a ? 50 : 40;
        
        cout << c;

        return 0;
    }

a and b are integers. The condition is if b is greater than a. If it is, 50 is returned, else 40 is return. The return value is assigned to the integer, c, which can be declared at that position. In this case either of the return values must be integers. Both return values should be of the same type, which does not only have to be an int. The type can be a float for example, or something else. The object the return value is assigned to must be of the same type as both of the returned values.

The ?: operator is a right-to-left operator.

The Comma Operator
Two expressions can be separated by a comma (,). In this case the left expression is evaluated and then the right expression is evaluated next. The result of the left expression (operand) is discarded even though it has been evaluated. The return value of the comma operator is the result and type of the right expression (value). The operation of the comma operator is from left to right. The following code segment illustrates the use of the comma operator,

#include <iostream>
using namespace std;

int main()
    {
        int a;
        int b;

        b = (a=3, a+2);
        
        cout << b;

        return 0;
    }

a and b are integers declared without any value assigned to any of them. Then you have the statement where the operands for the comma operator are in brackets. The left operand of the comma operator is “a=3”, which is evaluated but the result is not returned. When it is evaluated, the value of a becomes 3; no result is returned but in memory, the value of a is 3. The right operand of the comma operator is “a+2”, which is evaluated next. In memory, the value of a is already 3, so 2 is added by the right operand to the value, making the value of a in memory, 5. This value is return. The statement assigns it to b. So the value of b becomes 5.

The Function Call Operator
The function call operator is (). You must have used this operator. It is used when you are calling a function. If the called function needs arguments, the arguments go inside this operator. () is a left-to-right operator.

The [] Operator
The [] operator is used when creating arrays. You must have seen this before. [] is a left-to-right operator.

The Scope Operator
While writing a program, it is possible to have more than one identifier with the same name for different reasons. Such a situation should be avoided. The way to avoid this is to put your identifiers in what is called namespace. If in two different namespaces you have one identifier in each of the namespaces with the same name, there will be no problem, because to use any of the identifiers you have to precede each with the namespace name. Between the namespace name and the identifier, you type the scope operator (::). The following code illustrates this:

#include <iostream>
using namespace std;

namespace ns
    {
        int identA;
        int identB;
    }


int main()
    {
        ns::identA = 6;

        cout << ns::identA;

        return 0;
    }

A namespace begins with the reserved word, namespace. This is followed by the name you want for the namespace. Then you have curly braces. Inside the curly braces you can have declarations of the identifiers. To use any of these identifiers as in the main function above, you begin with name of the namespace, followed by the scope operator and then the identifier name.

The scope operator is a left-to-right operator.

Well, we have seen most of the C++ operators in this series. Let us stop here and continue in the next part of the series, where we shall look at what is called operator precedence and a few other things.

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