Operator in C++

Operator in C++ are following,

operator in C++

The same operator in C++ is used to write a code for those tasks in the program, just as we use the operator for the mathematical task.

In the programming world these operator in C++ are divided into different categories. C ++ offers different type of operator. Some of which are described below are operators.

Operator’s example and program are also given below-

arithmetic Operator in C++

These are basic operators. These operators use two or more variables to perform math relative simple task. These operators are present in all programming languages.

These are described in the table below-

operator Name Example
+ Addition a+b
Substraction a-b
* Multiplicaton a*b
/ Division a/b
% Modulus a%b

In program

int a= 3,b=4;
cout<<a+b; // print addition 7 (3+4)
cout<<a-b; // print addition 1 (3-4)
cout<<a*b; // print addition 12 (3*4)

The operators below will be used with the control statement.

Relational Operator in C++

These operators in C++ are used to compare two variables

Operator Name Example
< Less than a<b
<= Less than and equal to a<=b
> Greater than a>b
>= Greater than and equal to a>=b
== Equal to a==b
!= Not equal to a!=b

int a=3,b=4;
if(a<b) if 3 is greater than 4 than execute body of if
   // Body of if
if(a!= b) if a and b value is not equal than execute body of if
  // Body of if

Logical Operator

This type of operator lets us combine more than two or more conditions together.

Operator Name Example
&& AND a>5 && a<10
|| OR a>5 || a<10
! NOT !a

int a = 5, b = 10;
if(a>5 && b<10) // if a value is greater than 5 AND b value is smaller than 10 than execute body of   if
  //Body of if

if a value is greater than 5 OR b value is smaller than 10 than execute body of if(doesn’t matter if any one condition is false)
if(a>5 | | b<10) {
  //Body of if

if a value is greater than 5 OR b value is smaller than 10 than AND a value is also smaller than 10 than execute body of if
if(a>5 | | b<10 && a < 10 ) {
//Body of if

int c = 0;
if(!c) // if c value 0 than execute body of if
    //body of if

Because the value of variable of 0 in the NOT operator is the body of if execute i.e. it is like 0 ON and other different value OFF. Therefore, it is used mostly to point out a statement or function in the program.

special Operator and their types

This is not a different category operator, but it is a combination of two different or similar operator. Which are used for the different task. Here are some basic special operators and their assignments, as well as their program below –

Operator Name Description
= Assignment operator For value assign or variable initialization
+= Addition and assign In two variables, first assign the value of the left variable to the right variable, then the addition of the value of the right variable to the left variable.
-= Subtraction and assign same above
/= Division and assign same above
*= Multiplication and assign same above
%= Modulus and assign To divide the left variable with the right-side variable and then assign the reminder to the left variable.
++ increment increase value one by one
decrement decrease value one by one
& reference operator To print the address of a variable or to assign the value of one variable to another (using reference variable)
* de-reference operator To store the address of a variable
sizeof() sizeof operator To find the size of a data-type or a variable.
, comma operator For multiple variable declaration, initialization or multiple updation
:: scope resolution operator To change the scope of a variable and for the outline-definition of their function members in class and structure.
. dot operator To access the members of class and Structure
-> arrow operator In Pointer to Structure/class, to access their members
new new operator for dynamically memory allocation
delete delete operator used to de-allocate the  dynamically allocate memory.
endl new line feed operator To insert a new line in the program. Just like the keyboard has an enter button.

Here, some of these special operators used in below program,


 void main()
    int count,sum=0,mul=1; //variable initialization using assignment operator 

      for(count = 1; count<=5;count++)
         sum += count; // store addition of count into sum
         mul *= count; // store multiply of count into sum
    cout<<"Adition : "<<sum; // print variable sum value
    cout<<"\nMultiply: "<<mul<<endl; // print variable mul value

    cout<<"Address of count variable : "<<&count<<"\n"; // print address count
    cout<<"size of int data type(in byte): "<<sizeof(int); // print size of int 
Addition: 15
Multiply: 120

Address of count variable : 0x8f87fff4
Size of int data type(int byte): 2


scope resolution operator

This operator is used in the global variable. When a program has the same name as the local variable and the global variable, we use the scope resolution operator ( :: ) to access the value of the global variable in this state.

You can understand this from the program given below, where  two same name num variables have been declared in global and local and the scope operator has been used to access the value of the global declare variable-

#include <stdio.h>
#include <string.h>

 int num=3; // global initialization

  void main()
     int num = 6; //local initialization

     cout<<"local num : "<<num<<endl; // print local variable value
     cout<<"global ::num: "<<::num; // print global variable value
local num : 6
global ::num: 3

it is also used in, to define member function definition outside in the class and structure. Which is further explained in this tutorial.

previous-constant and their types

next-variable and their types