Operators in C++

 Operators in C++

All the C operators are valid in C++ also. C++ introduce some new operators like insertion operator (<<), extraction operator (>>), and scope resolution operator (::) etc.
some of thee new operator added to C in C++ are:

::*    pointer-to-member declarator
->*    pointer-to-member operator
.*    pointer-to-member operator
delete    memory release operator
new    memory allocation operator
endl    line feed operator
setw    field width operator

the different types of the operators which are common in C and C++ are as follows:
  • Arithmetic operator (+, -, *, /, %)
  • Logical operator (&&, ||, !)
  • Relational operator (<, <=, >, >=, ==, !=)
  • Assignment operator (=, +=, -=, *=, /=, %=)
  • Conditional operator (expression ? expression : expression)
  • Bitwise operator (&, |, ^, ~, <<, >>)
in C++ we can also add our own meaning to the existing operators depending upon the types of argument used, this process is known as operator overloading.

Arithmetic operator:

this are the basic arithmetic operators they we use in mathematics, they work pretty much same in the programming language, with addition of modulo operator (%) which returns the reminder of the division between two numbers. the other arithmetic operators are addition (+), subtraction (-), division (/) and multiplication (*).

Example of Arithmetic operator
#include <iostream>
using namespace std;
int main(){
  int num1 = 20;
  int num2 = 10;
  cout<<"num1 + num2: "<<(num1 + num2)<<endl;
  cout<<"num1 - num2: "<<(num1 - num2)<<endl;
  cout<<"num1 * num2: "<<(num1 * num2)<<endl;
  cout<<"num1 / num2: "<<(num1 / num2)<<endl;
  cout<<"num1 % num2: "<<(num1 % num2)<<endl;
  return 0;
}
output
num1 + num2: 30
num1 - num2: 10
num1 * num2: 200
num1 / num2: 2
num1 % num2: 0

Logical Operator:

These are mainly used in conditional statements and loops for evaluating a condition.
logical operator in C++ are as follows: logical and (&&). logical or (||) and logical not (!).

suppose if we have two statements A and B then in A && B will return true if both are true else it will return false.
in A || B will return true if any one of the A or B is true else it will return false.
in !A will return opposite of A, it means if A is true it will return false and vise a versa
in C++ true means 1 and false means 0
it want actually return true or false but it will return the 0 or 1 instead.

Example of logical operator:
#include <iostream>
using namespace std;
int main(){
   bool b1 = true;
   bool b2 = false;
   cout<<"b1 && b2: "<<(b1&&b2)<<endl;
   cout<<"b1 || b2: "<<(b1||b2)<<endl;
   cout<<"!(b1 && b2): "<<!(b1&&b2);
   return 0;
}
output:
b1 && b2: 0
b1 || b2: 1
!(b1 && b2): 1

Relational operator:

This operators are used to compare two operands, there are six relational operators as follows:
  • == returns true if both the left side and right side are equal
  • != returns true if left side is not equal to the right side of operator.
  • > returns true if left side is greater than right.
  • < returns true if left side is less than right side.
  • >= returns true if left side is greater than or equal to right side.
  • <= returns true if left side is less than or equal to right side.
Example of Relational operator:
#include <iostream>
using namespace std;
int main(){
   int num1 = 20;
   int num2 =10;
   if (num1==num2) {
      cout<<"num1 and num2 are equal"<<endl;
   }
   if( num1 != num2 ){
      cout<<"num1 and num2 are not equal"<<endl;
   }
   if( num1 > num2 ){
      cout<<"num1 is greater than num2"<<endl;
   }
   if( num1 >= num2 ){ 
      cout<<"num1 is greater than or equal to num2"<<endl;
   }
   if( num1 < num2 ){
      cout<<"num1 is less than num2"<<endl;
   }
   if( num1 <= num2){
      cout<<"num1 is less than or equal to num2"<<endl;
   }
   return 0;
}
output:
num1 and num2 are not equal
num1 is greater than num2
num1 is greater than or equal to num2

Assignment operator:

This operator are used to assign values to the variables. they are as follows:
=, +=, -=, *=, /=, %=

  • num2 = num1 would assign value of variable num1 to the variable.
  • num2+=num1 is equal to num2 = num2+num1
  • num2-=num1 is equal to num2 = num2-num1
  • num2*=num1 is equal to num2 = num2*num1
  • num2/=num1 is equal to num2 = num2/num1
  • num2%=num1 is equal to num2 = num2%num1
Example of Assignment operators:
#include <iostream>
using namespace std;
int main(){
 int num1 = 20;
 int num2 = 10;
 num2 = num1;
 cout<<"= Output: "<<num2<<endl;
 num2 += num1;
 cout<<"+= Output: "<<num2<<endl;
 num2 -= num1;
 cout<<"-= Output: "<<num2<<endl;
 num2 *= num1;      
 cout<<"*= Output: "<<num2<<endl;
 num2 /= num1;      
 cout<<"/= Output: "<<num2<<endl;
 num2 %= num1;      
 cout<<"%= Output: "<<num2<<endl;
 return 0;
}
output:
= Output: 20
+= Output: 40
-= Output: 20
*= Output: 400
/= Output: 20
%= Output: 0

Conditional operator:

this is also known as ternary operator, expression1 ? expression2 : expression3
if the expression1 is true then the expression2 will be evaluated and if it is false then expression3 will be evaluated.
expression2 and expression3 can be any expression which returns some thing or they can be constants.
Example of conditional operator:
#include <iostream>
using namespace std;
int main(){
  int num1, num2; num1 = 9;
  num2 = (num1 == 1) ? 100: 200;
  cout<<"num2: "<<num2<<endl;
  num2 = (num1 == 9) ? 100: 200;
  cout<<"num2: "<<num2;
  return 0;
}
output:
num2: 200
num2: 100

Bitwise operator:

there are six types of bitwise operator, this operator work on the bit level, they work only with the integers. they convert the numbers to binary then the operation are performed on them. the bitwise operators are as follows:
bitwise and (&), bitwise or (|), bitwise x-or (^), bitwise not (~), left shift (<<), right shift (>>).

num1 & num2 compares corresponding bits of num1 and num2 and generates 1 if both bits are equal, else it returns 0.
num1 | num2 compares corresponding bits of num1 and num2 and generates 1 if either bit is 1, else it returns 0.
num1 ^ num2 compares corresponding bits of num1 and num2 and generates 1 if they are not equal, else it returns 0.
~num1 is a complement operator that just changes the bit from 0 to 1 and 1 to 0.
num1 << 2 is left shift operator that moves the bits to the left, discards the far left bit, and assigns the rightmost bit a value of 0. 
num1 >> 2 is right shift operator that moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0.
Example of bitwise operator:
#include <iostream>
using namespace std;
int main(){
   int num1 = 11;  /* 11 = 00001011 */
   int num2 = 22;  /* 22 = 00010110 */ 
   int result = 0;
   result = num1 & num2;
   cout<<"num1 & num2: "<<result<<endl;
   result = num1 | num2;
   cout<<"num1 | num2: "<<result<<endl;
   result = num1 ^ num2;
   cout<<"num1 ^ num2: "<<result<<endl;
   result = ~num1;
   cout<<"~num1: "<<result<<endl;
   result = num1 << 2;
   cout<<"num1 << 2: "<<result<<endl;
   result = num1 >> 2;
   cout<<"num1 >> 2: "<<result;
   return 0;
}
output:
num1 & num2: 2
num1 | num2: 31
num1 ^ num2: 29
~num1: -12
num1 << 2: 44 num1 >> 2: 2

Comments

Post a Comment

if you have any doubts, let me know

Popular posts from this blog

Datatypes In C++

Implicit And Explicit Type Conversion