Difficulty: Beginner
Programming Language: C++

Article Index

Types Of Operators

So far we've seen how to create a new variable of a specific type and give it an identifier we can use later in the program. We know how to assign a value to this variable, and we have a basic overview of the different variable types available in C++. Now we can start to make C++ variables more useful by using operators on them in order to perform calculations and give us meaningful results.

An operator is a token in C++ that takes one to three operands, performs a useful operation and returns the result. There are three forms of operator, which are named after the number of parameters (operands) they use in order to perform their task. These are known as called unary, binary or ternary operators. Unary operators take a single operand to do its work, binary operators take two operands, and ternary operators take three operands.

Usually though in C++ and other similar languages it makes more sense to group the operators by their function instead of simply the number of arguments they take. Here we've decided to group the operators available to C++ into three categories: Logical operators; Value operators; Bitwise operators.

Before we go over the operators we need to mention the use of brackets ( ), which may be used to specify the order in which operations occur - much like in mathematics. We will be using brackets in the following pages primarily to highlight the area of code we're looking at, but also to prevent conflicts with the console output stream. The reason for this conflict is due to the syntax when outputting to the console stream which uses one of the operators by overriding it's functionality when used with the stream. This overridden behaviour may lead to unintended behaviour when not accounted for. When we wrap our operation within a bracket, this will be performed first so the output stream does not override the action we intended to perform.