Once we know of the existence of variables and constants, we can begin to operate with them. For that purpose, C++ integrates operators. Unlike other languages whose operators are main keywords, operators in C++ are mostly made of signs that are not part of the alphabet but are available in all keyboards. This makes C++ code shorter and more international, since it relies less on English words, but requires a little of learning effort in the beginning.
You do not have to memorize all the content of this page. Most details are only provided to serve as a later reference in case you need it.
They are five arithmetic operators in C++.
- + Addition or unary plus
- - Subtraction or unary minus
- * Multiplication
- / Division
- % Modulo operator
These operators can operate on any arithmetic operations in C++.
- It Compares two operands and depending on their relationship.
Op1 Operator Op2
a > b
There are six relational operators. They are,
- < less than
- > greater than
- <= less than or equal to
- >= greater than or equal to
- == equal to
- != not equal to
- AND, OR operators are used when we want to use two or more Conditions.
Types Of Logical Operators
- && Logical AND
- || Logical OR
- ! Logical NOT
Logical And (&&) Operator
Logical And Operator Definition
If both the operations are successful, then the condition becomes true.
Logical And Operator Syntax
expr1 && expr2
Logical And Operator Syntax Example
if( (a>10) && (a<20) ) printf(?A is in-between of 10 and 20?);
They are used to assign the result of an expression to a variable.
identifier = Expression
There are five assignment operators. They are,
a=a+1 a+=1 a=a- 1 a-= 1 a=a*2 a*=2 a=a/2 a/= 2 a=a%2 a%=2
- Left-hand side operator need not repeat.
- Easy to Read
- More Efficient
There are two Unary Operators. They are Increment and Decrement.
Increment Unary Operator
Is Equivalent i=i+1 or i+=1
Increment Unary Operator Types
- Post Increment i++
- Pre Increment ++i
Decrement Unary Operator
Is Equivalent i=i-1 or i-=1
Decrement Unary Operator Types
- Post Decrement i--
- Pre Decrement --i
Unary Operators Explanation
- ++i: increments l and then uses its value as the value of the expression;
- i++: uses l as the value of the expression and then increments l;
- --i: decrements l and then uses its value as the value of the expression;
- i--: uses l as the value of the expression and then decrements l.
- Change their original value.
Conditional or Ternary operator
Check condition if true, it returns first variable value otherwise return second values. sometimes it replaces if..else statement
Condition? Expression1: Expression2
(a>10) ? b : c
Explanation For Conditional or Ternary operator
a, b, c
(a>10) ? b : c
has as its value b if a is nonzero, and c otherwise. Only expression b or c is evaluated.
Expressions b and c must be of the same data type. If they are not but are both arithmetic data types, the usual arithmetic conversions are applied to make their types the same. It is also called ternary operators.
The Comma Operator
- The Comma operator can be used to link the related expressions together.
Example For Comma Operator
Comma Operator In for Loops for(i=0,j=1;i>10:i++,j++)
Comma Operator In while Loops
Scope Resolution Operator
The scope resolution operator is used for the Unary scope operator if a namespace scope (or) Global Scope
Scope Resolution Operator Syntax
:: identifier // for Global Scope class-name :: identifier // for Class Scope namespace :: identifier // for Namespace Scope //simple syntax :: global variable name
For more information on Scope Resolution Operator, check this link
new Memory Allocation Operator
- In C++, new Operator is used to allocating memory at runtime.
data_type *ptr; ptr = new data_type[size];
delete Memory Releasing Operator
- In C++, delete operator, is used to release memory or de-allocates memory that was previously allocated by the new operator at runtime or end of the program.
delete  ptr;
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Function Overloading Using C++ Programming
- Factorial Using Function Example Program In C++