Operators#

C++ defines a lot of operators that can be used on objects. We’ll discuss them here in the context of math with numeric types, but these operators can be extended to work with other types as well.

Mathematical operators#

The standard mathematics operators are:

  • + : addition

  • - : subtraction

  • * : multiplication

  • / : division

  • % : modulus

For example, to compute the area and perimeter of a rectangle, we could do:

Listing 3 rectangle.cpp#
#include <iostream>

int main() {

    double width = 4.0;
    double height = 2.5;

    double area = width * height;
    double perimeter = 2 * width + 2 * height;

    std::cout << "area = " << area << std::endl;
    std::cout << "perimeter = " << perimeter << std::endl;

}

In addition, + and - can be used as unary operators, e.g., to negate a quantity:

double x = -y;

Note

Unlike in some languages, there is no operator for exponentiation, e.g., \(x^y\). We’ll see how to call a function to this shortly.

Relational operators#

Relational operators compare objects. The main operators are:

  • == : equal to

  • != : not equal to

  • > : greater than

  • < : less than

  • >= : greater than or equal to

  • <= : less than or equal to

For example:

Listing 4 relations.cpp#
#include <iostream>

int main() {

    double x = 1.2;
    double y = 2.0;

    std::cout << "x > y is " << (x > y) << std::endl;
    std::cout << "x < y is " << (x < y) << std::endl;
    std::cout << "x == y is " << (x == y) << std::endl;
    std::cout << "x != y is " << (x != y) << std::endl;


}

Note

C++20 introduces a three-way comparison, <=>, which we will not consider.

Assignment operators#

Assignment modifies the value of the object to the left of the operator. The assignment operators include:

  • a = b : set the value of a to that of b

  • a += b : equivalent to a = a + b

  • a -= b : equivalent to a = a - b

  • a *= b : equivalent to a = a * b

  • a /= b : equivalent to a = a / b

Listing 5 assignment.cpp#
#include <iostream>

int main() {

    double x{10};

    x += 1;

    std::cout << "x is now: " << x << std::endl;

    x *= 10;

    std::cout << "x is now: " << x << std::endl;

}

Precedence#

What happens when we have an expression like:

a + b * c;

The language defines the order that operators are considered, called operator precedence. This table: C++ Operator Precedence lists the order in which operators are evaluated.

For the operators that we’ve seen so far, here is an (abbreviated) precedence order:

  • -a, +a : unary operators

  • a * b, a / b, a % b : multiplication / division / remainder

  • a + b, a - b : addition / subtraction

  • a = b : assignment

In the example above, multiplication has higher precedence, so it is evaluated first, and then the addition is done.

Tip

We can use parentheses to force a group to be evaluated first, e.g., (a + b) * c

Try it…

Play around with some expressions to explore the precedence of operators.

Prefix and postfix operators#

C++ also has the increment and decrement (postfix) operators.

  • a++ is equivalent to a = a + 1

  • a-- is equivalent to a = a - 1

There are prefix versions of these, e.g., ++a. The difference between them is subtle.

The prefix operator is:

int a{0}, b{0};
b = ++a;

The prefix increment operator first increments the value of the object and then returns a reference to the result. So in the above example, the result would be b = 1.

Now consider the postfix operator:

int c{0}, d{0};
d = c++;

Here, a copy of c is made, then c is incremented, and finally the copy is returned. So in the above example, the result would be d = 0.

Associativity#

When an expression has two operators at the same precedence, operator associativity rules come into play. Most operators in C++ are left associative—that is, they are grouped from the left, but some are right associative (like the unary operators and assignment) meaning that they are grouped from the right.

Consider assignment:

a = b = c

Since = has right associativity, we interpret this as a = (b = c), which can further be thought of as b = c; a = c.

Note

In C++, assignment evaluates to the left value.

In the expression:

a + b - c

Both + and - have the same precedence, and these are left-associative, so first a + b is evaluated and then c is subtracted from that result.