Operators in C++

by Nideesh C on May 7, 2011 · 0 comments

in C++




Operators are used in C++ programs to carry out calculations or relations for the specified values. C++ supports the following types of operators

Arithmetic Operators

They are used to execute a given set of arithmetic expressions. The expression is evaluated using the following operators.

Operator Meaning
+ Add Values
- Substrate Values
* Multiply Values
/ Divide Values
% Modular Division of Values

Syntax:

<Value/Variable><Operator><Value/Operator>

Eg: 8+8, 18/9, A*B,X*30 Etc

/*——————————————————————————*/

#include<iostream.h>

#include<conio.h>

void main()

{

int a,b;

cout<<”Enter the values for a and b :” ;

cin>>a>>b;

cout<<endl<<”a+b=”<<a+b;

cout<<endl<<”a-b=”<<a-b;

cout<<endl<<”a*b=”<<a*b;

cout<<endl<<”a/b=”<<a/b;

cout<<endl<<”a%b=”<<a%b;

getch();

}

/*—————————————————————————*/

Output:

Enter values for a and b : 40     10

a+b=50

a-b=30

a*b=400

a/b=4

a%b=0

Relational operators

These operators are used to check the relation between two values, such as, whether a value is grater than the other

Operator Meaning
> Greater than
>= Greater than or equal
< Lesser than
<= Lesser than or equal
== Equal to
!= Not equal to

Syntax:

<Value/Variable><Operator><Value/Variable>

Eg: A>b,X==y Etc

/*—————————————————————————————————-*/

#include<iostream.h>

#include<conio.h>

void main()

{

int X,Y;

cout<<”Enter values for X and Y”:;

cin>>X>>y;

if (X>Y)

cout<<endl<<”X is big”;

else

cout<<endl<<”y is big”;

getch();

}

/*—————————————————————————————*/

Output:

Enter values for X and Y :  10    40

Y is big

Logical operators

A logical operator is used to combine more than one relation and check the result (True or False).

Operator Meaning
&& And
|| Or
! Not

“&&” returns a true value if both relations are true else it returns a false value.An ” || ” operator return to true if any one of the condition is true else it will return a false value if both the conditions are false. The ” ! ” operator return a true value if the expression returns to false.

Syntax:

<Relation1><operator><Relation2>

Eg: (a>b) && (a>c)

/*——————————————————————————————*/

#include<iostream.h>

#include<conio.h>

void main()

{

int X;

cout<<”Enter values for X “:;

cin>>X;

if ((x>0)&&(x<20))

cout<<endl<<”valid Input”;

getch();

}

/*———————————————————————————————-*/

Outoput:

Enter a value for X : 15

Valid Input

Assignment Operator

The operator “=” is used as the assignment operator to initialize values to the variables.

Syntax:

<Variable>=<Value>;

Eg:

x = 100

ch = ‘z’;

etc

The assignment operator can also be used with the arithmetic operators,which names it as the short hand assignment operator.

Syntax:

<Variable><Arthmetic Operator>=<Value>;

Eg:

X+=100;  is same as X=X+100;

X*=20;     is same as X=X*20;

Etc

Incriment and Decriment Operators

The ” ++ ” and ” — ” are said to the increment and decrement operators, used to increase a variable value by 1.

Syntax

<Variable><operator>;

Eg:

ch++;

z–;

Etc

/*————————————————————————-*/

#include<iostream.h>

#include<conio.h>

void main();

{

int x;

cout<<”Enter value for x : “;

cin>>x;

cout<<endl<<”Incremented value is :”<<X++;

cout<<endl<<Decremented valu is : “<<X–;

getch();

}

/*———————————————————————————————-*/

Output:

Enter value for X : 100

Incremented value is : 101

Decremented value is : 100

Bitwise Operators

The operators ” & “,  ” | “, “~”, “<<”, “>>” and “^” are said to be bitwise operators,which operate upon the bits values (0′s and 1′s)

of a given expression.

Conditional Operator

In C++ ” ?: ” is known as conditional operator, which is the short cut for the simple if ……else construct.

Syntax

<Condition>?<Execute on True>:<Execute on False>

Eg:

int X;

X=(a>b)?a:b;

Scope resolution operator ” :: ”

The operator is used to specify the scope of variables and functions of a class (i.e. at what places they should be used).

Member reference operators

” -> ” Known as the ” Arrow ” operators is used by the class object to access the member functions of another class/s, which makes the method of “Late Binding”, wherein the concept of inheritance is applied.

Memory manipulation operators

For an effective memory management with C++, operators ” new ” and ” delete “are used to allocated necessary memory and remove the allocated memory at runtime.

Type cast operators

These operators are used to convert data of one type to the data of another type.

syntax:

<Conversion Data Type>(<Variable>)

Usage:

<Variable>=<Conversion Data Type>(<Variable>);

Eg:

flot X = 34.56;

int y;

y=int(X);            // flot value converted to int for y


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. c++ Program – illustrate the use of difference operators using friend function
  2. Java program – Binary search (20 questions)
  3. if statement in C++
  4. Sample – functions
  5. C++ program – Perform arithmetic operations of two complex numbers using operator overloading

Leave a Comment

Previous post:

Next post: