Assignment (=):
The assignment operator assigns a value to a variable.a=5;

This statement assigns the integer value 5 to the variable a. The part at the left of the assignment operator (=) is known as the lvalue (left value) and the right one as the rvalue (right value). The lvalue has to be a variable whereas the rvalue can be a constant, a variable, the result of an operation or any combination of these. The most important rule when assigning is the right-to-left rule:

The assignment operation always takes place from right to left, and never the other way:
a= b;

This statement assigns to variable a (the lvalue) the value contained in variable b (the rvalue). The value that was stored until this moment in a is not considered at all in this operation, and in fact that value is lost.

Arithmetic operators ( +, -, *, /, % )
The five arithmetical operations supported by the C++ language are:
+ addition
* multiplication
/ division
% modulo

Operations of addition, subtraction, multiplication and division literally correspond with
their respective mathematical operators. The only one that you might not be so used to see may be modulo; whose operator is the percentage sign (%). Modulo is the operation that gives the remainder of a division of two values. For example, if we write: a = 11 %3;

The variable a will contain the value 2, since 2 is the remainder from dividing 11 between 3.

Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
When we want to modify the value of a variable by performing an operation on the value currently stored in that variable we can use compound assignment operators:

expression is equivalent to

value += increase; value = value + increase;
a -= 5; a = a – 5;
a /= b; a = a / b;
price *= units + 1; price = price * (units + 1);

Increase and decrease (++, –)

Shortening even more some expressions, the increase operator (++) and the decrease operator (–) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively.

Thus: c++; c+=1; c=c+1; are all equivalent in its functionality: the three of them increase by one the value of c.
A characteristic of this operator is that it can be used both as a prefix and as a suffix.That means that it can be written either before the variable identifier (++a) or after it (a++).

Although in simple expressions like a++ or ++a both have exactly the same meaning, in other expressions in which the result of the increase or decrease operation is evaluated as a value in an outer expression they may have an important difference in their meaning:

In the case that the increase operator is used as a prefix (++a) the value is increased before the result of the expression is evaluated and therefore the increased value is considered in the outer expression;

in case that it is used as a suffix (a++) the value stored in a is increased after being evaluated and therefore the value stored before the increase operation is evaluated in the outer expression.

Relational and equality operators ( ==, !=, >, <, >=, <= ):
In order to evaluate a comparison between two expressions we can use the relational and equality operators. The result of a relational operation is a Boolean value that can only be true or false, according to its Boolean result. We may want to compare two expressions, for example, to know if they are equal or if one is greater than the other is. Here is a list of the relational and equality operators that can be used in C++:

Operators Meaning

== Equal to

!= Not equal to

> Greater than

< Less than >= Greater than or equal to

<= Less than or equal to Here there are some examples: (7 == 5) // evaluates to false. (5 > 4) // evaluates to true.

(3 != 2) // evaluates to true.

(6 >= 6) // evaluates to true.

(5 < 5) // evaluates to false. Of course, instead of using only numeric constants, we can use any valid expression,including variables. Suppose that a=2, b=3 and c=6, (a == 5) // evaluates to false since a is not equal to 5.

(a*b >= c) // evaluates to true since (2*3 >= 6) is true.

(b+4 > a*c) // evaluates to false since (3+4 > 2*6) is false.

((b=2) == a) // evaluates to true.

Be careful! The operator = (one equal sign) is not the same as the operator == (two equal signs), the first one is an assignment operator (assigns the value at its right to the variable at its left) and the other one (==) is the equality operator that compares whether both expressions in the two sides of it are equal to each other. Thus, in the last expression ((b=2) == a), we first assigned the value 2 to b and then we compared it to a, that also stores the value 2, so the result of the operation is true.

Logical operators ( !, &&, || ):

The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand.

For example:
!(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true.

!(6 <= 4) // evaluates to true because (6 <= 4) would be false.

!true // evaluates to false

!false // evaluates to true.

The logical operators && and || are used when evaluating two expressions to obtain a single relational result. The operator && corresponds with Boolean logical operation AND. This operation results true if both its two operands are true, and false otherwise.

The following panel shows the result of operator && evaluating the expression a && b:
a b a && b
true true true
true false false
false true false
false false false

The operator || corresponds with Boolean logical operation OR.
This operation results true if either one of its two operands is true, thus being false only
when both operands are false themselves. Here are the possible results of a || b:

a b a || b
true true true
true false true
false true true
false false false

For example:
( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false ).
( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ).

Conditional operator ( ? )

The conditional operator evaluates an expression returning a value if that expression is true and a different one if the expression is evaluated as false. Its format is:
condition ? result1 : result2

If condition is true the expression will return result1, if it is not it will return result2.
7==5 ? 4 : 3 // returns 3, since 7 is not equal to 5.
7==5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2.
5>3 ? a : b // returns the value of a, since 5 is greater than 3.
a>b ? a : b // returns whichever is greater, a or b.

Comma operator ( , ):
The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered. For example, the following code:
a = (b=3, b+2);

Would first assign the value 3 to b, and then assign b+2 to variable a. So, at the end, variable a would contain the value 5 while variable b would contain value 3

This operator accepts one parameter, which can be either atype or a variable itself and returns the size in bytes of that type or object:
a = sizeof (char);

This will assign the value 1 to a, because char is a one-byte long type. The value returned by sizeof is a constant, so it is always determined before program execution.

Some of the new operators in c++ are-
:: Scope resolution operators.
::* pointer to member decelerator.
* pointer to member operator.
.* pointer to member operator.
delete memory release operator.
endl Line feed operator
new Memory allocation operator.
stew Field width operator.

Scope resolution Operator.
1) C++ is a block – structured language. The scope of the variable extends from the point of its declaration till the end of the block containing the declaration.


Consider following program.

{ int x = 1; 
{ int x = 2;
} ======

The two declaration of x refer to two different memory locations containing different values. Blocks in c++ are often nested. Declaration in a inner block hides a declaration of the same variable in an outer block.

3) In C, the global version of a variable cannot be accessed from within the inner block. C++ resolves this problem by using scope resolution operator (::), because this operator allows access to the global version of a variable.

5) A major application of the scope resolution operator is in the classes to identify the class to which a member functions belongs.

Member dereferencing operators .
C++ permits us to define a class containing various types of data & functions as members. C++ also permits us to access the class members through pointers. C++ provides a set of three pointer.

C++ provides a set of three pointers to member operators.
1) ::* – To access a pointer to a member of a class.
2) .* – To access a member using object name & a pointer to that member.
3) * – To access a member using a pointer in the object & a pointer to the member.

Memory management operators.
1) We use dynamic allocation techniques when it is not known in advance how much of memory space is needed. C++ supports two unary operators new and delete that perform the task of allocating & freeing the memory.

2) An object can be created by using new and destroyed by using delete. A data object created inside a block with new, will remain existence until it is explicitly destroyed by using delete.

3) It takes following form.
variable = new data type
The new operator allocated sufficient memory to hold a data object of type data-type & returns the address of the object.
EX p = new int.

Where p is a pointer of type int. Here p must have already been declared as pointer of appropriate types.

4) New can be used to create a memory space for any data type including user defined type such all array, classes etc.
Ex int * p = new int [10]; Creates a memory space for an array of 10 integers.

5) When a data object is no longer needed, it is destroyed to release the memory space for reuse. The general form is delete variable. If we want to free a dynamically allocated array, we must use following form. delete [size] variable; The size specifies the no of elements in the array to be freed.

6) The new operator has following advantages over the function malloc() in c -.
i) It automatically computes the size of the data object. No need to use sizeOf()
ii) If automatically returns the correct pointer type, so that there is no need to use a type cast.
iii) new and delete operators can be overloaded.
iv) It is possible to initialize the object while creating the memory space.

Manipulators are operators that are used to format the data display.

There are two important manipulators.
1) endl
2) stew
1) endl :

This manipulator is used to insert a linefeed into an output. It has same effect as using .\n. for newline.

2) Setw :
With the stew, we can specify a common field width for all the numbers and force them to print with right lignment.

EX cout<

The manipulator setw(5) specifies a field width of 5 for printing the value of variable sum the value is right justified.
3 5 6
Type cast operator.
C++ permits explicit type conversion of variables or expressions using the type cast operator.
Syntax . type name (expression)
Ex avg = sum/float(i)
Here a type name behaves as if it is a function for converting values to a designated type.

Share with : Share on Linkedin Share on Twitter Share on WhatsApp Share on Facebook