In this chapter we shall learn about following topics:
- C++ qualifiers
- C++ Operators
1. C++ qualifiers
C++ provides one of two qualifiers.
- Volatile: When a variable is defined as volatile, then the value can be changed. By default, if you create a variable, it is declared as a “volatile” variable.
“volatile” is the keyword.
Example:
volatile int num = 10; num = 20; // correct
- Constant: when a variable is declared as constant, the value assigned to that variable cannot be changed.
“const” is the ketword.
Example:
const int num = 10; num = 20; // error. as int is a constant.
2. C++ Operators
Below are the types of operators in CPP:
- Relational Operators
- Logical Operators
- Arithmetic Operators
- Bitwise Operators
- Shift Operators
- Unary Operators
- Assignment Operators
-
Relational Operators:
Relational operators are used to know a relation between 2 variable. Below are the operators available.
== checks if two values are same. If same it will return true. != checks if both variables are not equals to each other. Then it will return true. > Checks if the value of Left is greater than the value to right. Then it will return true < Checks if the value of Left is lesser than the value to right. Then it will return true. >= Checks if the value of Left is greater than or euqall the value to right. Then it will return true. <= Checks if the value of Left is lesser than or equall to the value to right. Then it will return true.
Example for relational Operators in C++
/* * File : Relational _operators.cpp * Author : ajay.thousand@gmail.com * Copyright: @ prodevelopertutorial.com */ #include<iostream> using namespace std; int main() { int num_1 = 10; int num_2 = 20; cout<<"The result for Relational Operators will always be 0 and 1. 0 means False and 1 means true"<<endl; cout<<"The value of "<<num_1 << " == " << num_2<< " is = "<<(num_1 == num_2)<<endl; cout<<"The value of "<<num_1 << " <= " << num_2<< " is = "<<(num_1 <= num_2)<<endl; cout<<"The value of "<<num_1 << " >= " << num_2<< " is = "<<(num_1 >= num_2)<<endl; cout<<"The value of "<<num_1 << " != " << num_2<< " is = "<<(num_1 != num_2)<<endl; cout<<"The value of "<<num_1 << " < " << num_2<< " is = "<<(num_1 < num_2)<<endl; cout<<"The value of "<<num_1 << " > " << num_2<< " is = "<<(num_1 > num_2)<<endl; return 0; }
Output:
The value of 10 == 20 is = 0 The value of 10 <= 20 is = 1 The value of 10 >= 20 is = 0 The value of 10 != 20 is = 1 The value of 10 < 20 is = 1 The value of 10 > 20 is = 0
2. Logical Operators
Logical operators give the output based on the two statements. Below are the operators available:
- && logical and operator.
If Both the statements are true, then the condition is true. If any one of the statement is false, then it will return false.
Example:
int a = 10; int b = 20; int c = 30; if( c>b && c > a ) // This condition is true
- || logical or operator. This will return true if any one condition is satisfied.
if( a>b || c > a )
Here first condition is false, but second condition is true. As we are using “or” operator, the result is true.
- ! logical not operator.
It will inverse the result.
if (!true) result will be false.
Example for logical operator in C++
/* * File : Logical_operators.cpp * Author : ajay.thousand@gmail.com * Copyright: @ prodevelopertutorial.com */ #include<iostream> using namespace std; int main() { int a = 10; int b = 20; int c = 30; cout<<"The result for Logical Operators will always be 0 and 1. 0 means False and 1 means true"<<endl; cout<<"The value of ( c > b && c > a ) is = " << ( c>b && c > a ) <<endl; cout<<"The value of ( a>b || c > a ) is = " << ( a > b || c > a ) <<endl; cout<<"The value of ( !true ) is = " << ( !true ) <<endl; cout<<"The value of ( !false ) is = " << ( !false ) <<endl; return 0; }
Output:
The result for Logical Operators will always be 0 and 1. 0 means False and 1 means true The value of ( c > b && c > a ) is = 1 The value of ( a>b || c > a ) is = 1 The value of ( !true ) is = 0 The value of ( !false ) is = 1
-
Arithmetic Operators
+ Addition operator - Subtraction Operator * Multiplication Operator / Division Operator % Modulus Operator
Example for athematic operators in C++
/* * File : Arithmetic_operators.cpp * Author : ajay.thousand@gmail.com * Copyright: @ prodevelopertutorial.com */ #include<iostream> using namespace std; int main() { int num_1 = 10; int num_2 = 20; cout<<"The value of "<<num_1 << " + " << num_2<< " is = "<<(num_1 + num_2)<<endl; cout<<"The value of "<<num_1 << " - " << num_2<< " is = "<<(num_1 - num_2)<<endl; cout<<"The value of "<<num_1 << " / " << num_2<< " is = "<<(num_1 / num_2)<<endl; cout<<"The value of "<<num_1 << " * " << num_2<< " is = "<<(num_1 * num_2)<<endl; cout<<"The value of "<<num_1 << " % " << num_2<< " is = "<<(num_1 % num_2)<<endl; return 0; }
Output:
The value of 10 + 20 is = 30 The value of 10 - 20 is = -10 The value of 10 / 20 is = 0 The value of 10 * 20 is = 200 The value of 10 % 20 is = 10
4. Bitwise Operators
& Bitwise And | Bitwise Or ^ Bitwise NOT >> Bitwise Left shift << Bitwise Right Shift ~ Ones complement
Example for Bitwise operators in C++
/* * File : Bitwise_operators.cpp * Author : ajay.thousand@gmail.com * Copyright: @ prodevelopertutorial.com */ #include<iostream> using namespace std; int main() { int num_1 = 10; int num_2 = 20; cout<<"The value of "<<num_1 << " & " << num_2<< " is = "<<(num_1 & num_2)<<endl; cout<<"The value of "<<num_1 << " | " << num_2<< " is = "<<(num_1 | num_2)<<endl; cout<<"The value of ~ " << num_1<< " is = "<<~num_1<<endl; cout<<"The value of " << num_1<< " << 2 is = "<< num_1<<2 <<endl; cout<<"The value of " << num_1 << " >> 2 is = "<< (num_1>>2) <<endl; return 0; }
Output:
The value of 10 & 20 is = 0 The value of 10 | 20 is = 30 The value of ~ 10 is = -11 The value of 10 << 2 is = 102 The value of 10 >> 2 is = 2
6. Unary Operators
++ Increment Operator -- Decrement Operator
Increment Operator:
There are 2 types:
- Pre-Increment (++num). Here the value will be incremented then the value will be assigned.
- Post-Increment (num++). Here the value will be assigned and then incremented.
- Pre-Decrement (–num). Here the value will be decremented by one then the value will be assigned.
- Post -Decrement (num–). Here the value will be assigned and then decremented by one.
Example for Unary Operators in C++
/* * File : Unary_operators.cpp * Author : ajay.thousand@gmail.com * Copyright: @ prodevelopertutorial.com */ #include<iostream> using namespace std; int main() { int num_1 = 1; int num_2 = 1; int num_3 = 1; int num_4 = 1; cout<<"The value of num_1++ is = "<< num_1++<<endl; cout<<"The value of num_2-- is = "<< num_2--<<endl; cout<<"The value of ++num_3 is = "<< ++num_3<<endl; cout<<"The value of --num_4 is = "<< --num_4<<endl; return 0; }
Output:
The value of num_1++ is = 1 The value of num_2-- is = 1 The value of ++num_3 is = 2 The value of --num_4 is = 0
7. Assignment Operators
= Equal to operator += Add and assign operator -= Subtract and assign operator *= Multiply and assign operator /= Divide and assign operator <<= Left shift and assign operator >>= Right Shift and assign operator &= “Bitwise And” and assign operator |= Bitwise Or and assign operator ^= Bitwise exclusive or and assign operator
Example for Assignment Operators in C++
/* * File : Relational _operators.cpp * Author : ajay.thousand@gmail.com * Copyright: @ prodevelopertutorial.com */ #include<iostream> using namespace std; int main() { int num_1 = 10; cout<<"The value of "<<num_1 << " = " << 20<< " is = "<<(num_1 = 20)<<endl; cout<<"The value of "<<num_1 << " += " << 20<< " is = "<<(num_1 += 20)<<endl; cout<<"The value of "<<num_1 << " -= " << 20<< " is = "<<(num_1 -= 20)<<endl; cout<<"The value of "<<num_1 << " *= " << 20<< " is = "<<(num_1 *= 20)<<endl; cout<<"The value of "<<num_1 << " /= " << 20<< " is = "<<(num_1 /= 20)<<endl; cout<<"The value of "<<num_1 << " <<= " << 20<< " is = "<<(num_1 <<= 20)<<endl; cout<<"The value of "<<num_1 << " >>= " << 20<< " is = "<<(num_1 >>= 20)<<endl; cout<<"The value of "<<num_1 << " &= " << 20<< " is = "<<(num_1 &= 20)<<endl; cout<<"The value of "<<num_1 << " |= " << 20<< " is = "<<(num_1 |= 20)<<endl; cout<<"The value of "<<num_1 << " ^= " << 20<< " is = "<<(num_1 ^= 20)<<endl; return 0; }
Output:
The value of 10 = 20 is = 20 The value of 20 += 20 is = 40 The value of 40 -= 20 is = 20 The value of 20 *= 20 is = 400 The value of 400 /= 20 is = 20 The value of 20 <<= 20 is = 20971520 The value of 20971520 >>= 20 is = 20 The value of 20 &= 20 is = 20 The value of 20 |= 20 is = 20 The value of 20 ^= 20 is = 0