Answer to Question #188068 in C++ for Rida saeed

Question #188068

How many types of operator used in c++? Elaborate each operator with one example as a program.


1
Expert's answer
2021-05-02T06:07:06-0400

There are 9 type of operators in C++.These are as follows:

1- Assignment operator

2- Mathematical operator

3- Relational operator

4- Logical operator

5- Bitwise operator

6- Shift operator

7- Unary operator

8- Ternary operator

9- Comma operator


Definitions:


1- Assignment operator - Operates '=' is used for assignment, it takes the right-hand side (called r value) and copy it into the left-hand side (called l value). Assignment operator is the only operator which can be overloaded but cannot be inherited.


Example:


int x = 10;

x += 5;



2 - Mathematical operator - There are operators used to perform basic mathematical operations. Addition (+) , subtraction (-) , diversion (/) multiplication (*) and modulus (%) are the basic mathematical operators. 


Example:


#include <iostream>
using namespace std;
 
main() {
   int a = 21;
   int b = 10;
   int c ;
 
   c = a + b;
   cout << "Line 1 - Value of c is :" << c << endl ;
   
   c = a - b;
   cout << "Line 2 - Value of c is  :" << c << endl 
   ;
   c = a * b;
   cout << "Line 3 - Value of c is :" << c << endl ;
   
   c = a / b;
   cout << "Line 4 - Value of c is  :" << c << endl ;
   
   c = a % b;
   cout << "Line 5 - Value of c is  :" << c << endl ;
   
   c = a++; 
   cout << "Line 6 - Value of c is :" << c << endl ;
   
   c = a--; 
   cout << "Line 7 - Value of c is  :" << c << endl ;
   
   return 0;
}

The above example contains all the mathematical operators use in the C++.


3 - Relational operator - These operators establish a relationship between operands. The relational operators are : less than (<) , grater thatn (>) , less than or equal to (<=), greater than equal to (>=), equivalent (==) and not equivalent (!=).


Example:


int x = 10;
int y = 15;
int z = 10;

x == y   // false
x == z   // true

4 - Logical operator - The logical operators are AND (&&) and OR (||). They are used to combine two different expressions together.

If two statement are connected using AND operator, the validity of both statements will be considered, but if they are connected using OR operator, then either one of them must be valid. These operators are mostly used in loops (especially while loop) and in Decision making.


Example:




#include <iostream>
using namespace std;

int main() {
    int a = 5;
    int b = 9;
  
    // false && false = false
    cout << ((a == 0) && (a > b)) << endl;
  
    // false && true = false
    cout << ((a == 0) && (a < b)) << endl;

    // true && false = false
    cout << ((a == 5) && (a > b)) << endl;

    // true && true = true
    cout << ((a == 5) && (a < b)) << endl;

    return 0;
}

5 - Bitwise operator - There are used to change individual bits into a number. They work with only integral data types like char, int and long and not with floating point values.


Example:


#include <iostream>
using namespace std;

int main() {
    // declare variables
    int a = 12, b = 25;

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "a & b = " << (a & b) << endl;

    return 0;
}

6 - Shift operator - Shift Operators are used to shift Bits of any variable. It is of three types,

  1. Left Shift Operator <<
  2. Right Shift Operator >>
  3. Unsigned Right Shift Operator >>>


Example:


#include<iostream>
using namespace std;
int main() {
   int a = 1, b = 3;
   
   // a right now is 00000001
   // Left shifting it by 3 will make it 00001000, ie, 8
   a = a << 3;
   cout << a << endl;
   
   // Right shifting a by 2 will make it 00000010, ie, 2
   a = a >> 2;
   cout << a << endl;
   return 0;
}

7 - Unary operator - These are the operators which work on only one operand. There are many unary operators, but increment ++ and decrement -- operators are most used.


Example:




#include <iostream>
using namespace std;

class Count {
   private:
    int value;

   public:

    // Constructor to initialize count to 5
    Count() : value(5) {}

    // Overload ++ when used as prefix
    void operator ++ () {
        ++value;
    }

    void display() {
        cout << "Count: " << value << endl;
    }
};

int main() {
    Count count1;

8 - Ternary operator - The ternary if-else ? : is an operator which has three operands.


Example:


#include <iostream>
#include <string>
using namespace std;

int main() {
  double marks;

  // take input from users
  cout << "Enter your marks: ";
  cin >> marks;

  // ternary operator checks if
  // marks is greater than 40
  string result = (marks >= 40) ? "passed" : "failed";

  cout << "You " << result << " the exam.";

  return 0;
}

9 - Comma operator - This is used to separate variable names and to separate expressions. In case of expressions, the value of last expression is produced and used.


Example:


#include <iostream>
using namespace std;

int main() {
   int i, j;
   
   j = 10;
   i = (j++, j+100, 999+j);

   cout << i;
   
   return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS