C plus plus Operators

C++ Operators

C++ Operators are special symbols which are used to perform operations on operands .
C++ is very rich in built-in operators. In fact it places more significance on operators than do most other computer languages. We can say that an operator is a symbol that causes the computer language. We can say that an operator is a symbol that causes the compiler to take an action.

C++ Operators has the following types:

1) Arithmetic (Mathematical) Operators
2) Relational Operators
3) Logical Operators
4) Assignment Operators
5) Increment and Decrement Operators
6) Bitwise Operators

1. Arithmetic (Mathematical) C++ Operators

Following table list C/C++’s arithmetic operators. The operators +,?,*,and/work as they do not most other computer language. You can apply them to almost any built-in data type. when you apply / to an integer or character, any remainder will be truncated . For example,7/2 will equal 3 in integer division.
The modules operator % also works in C/C++ as it does in other languages, yielding the remainder of an integer division. However, you cannot use it on.
 Operators Action Example Result + Addition 10+10 20 – Subtraction, Also may unary minus 20-10 10 / Division 20/4 5 * Multiplication 10*10 100 % Modulus(Remainder) 10%5 0

2. Relational C++ Operators

Relational operators are use to determine whether two numbers are equal, or if one is greater or less than the other. Every relational expression returns either 1(true) or 0(false).
Relational operators are also known as comparison operators.
There are six relational C++ operators: equals(==),less then(<),greater than(>),less than or equal to(<=), greater or equal to(>=),and not equals(!=).Table shows each relational operator, its use, and a simple code use.

 Operators Name Example Result == Equals 200 == 100 False != Not Equals 200! = 100 True > Greater Then 200>50 True >= Greater then or equals 200>=100 True < Less then 200<100 False <= Less then or equals 200<=100 False

3.Logical C++ Operators

Logical operators are used to join one or more relational expression.
Often you ask more than one relational question at a time.It is true that x is a greater then y,
And also true that y greater then Z?.
A program might need to determine that both of these conditions are true, or that some other condition is true, in order to take an action.

 Operator Operator name Example Hazard to || Logical OR 5<2||3>1 true ! Logical NOT !5>2 false && Logical AND 3>2&&2<8 true
Imagine a sophisticated alarm system that has this logic: if they door alarm sounds AND it is after six p.m. AND it is NOT a holiday, OR if it is a weekend, then call the police. The three logical operators of C++ are used to make this kind of evolution. These operators are listed in table.

4.Assignment C++ Operators

Assignment operators is used to assigns values into a variable or constant. And operand that can legally be on the left side of a assignment operator is called and 1value.
And operand that can be on the right side is called r-values. Constants r-values, but not all r-values are 1-values. Thus you can write
X = 45 ;
//ok
But you cannot legally write
45=x;
//error, not an 1-value!
And 1-value is operand that can be on the left side all an expression. An r-values than operand that can be on the right side of an expiration.
Note that all 1-values are r-values, but not all r-values are 1-values. An example of an r-values that is not an

1-value is a literal. Thus, you can write x=5; , but you can write 5=x;

5. Increment or Decrements C++ Operators

C/C++ includes two useful operators not generally found in other computer language. These are increment and decrements operators, ++ and — .
The operator ++ adds 1 to its operand, and suspects one. In other words:
X=x+1;
is the same as
++X;
And
X=x-1;
Is the same as
x–;
Both the increment and decrements operators may either precede (prefix) or follow (post-fix) the operand. For Example:
X=x+1;
Can be written
++x;
Or
X++;
There is, however, a difference between the prefix and post-fix forms when you use these operators is an expression. When an increment or decrements operator proceeded its operand , the increment and decrements operation is performed before obtaining the value of the operand for use in the expression. If the C++ operator follows its operand, the value of the operand is obtained before increment or decrements it.
For instance,
X=10;
Y=++x;
Sets y to 11.However, if you write the code as
X=10;
Y=x++;
y is set to 10.

6.Bit wise C++ Operators

Unlike many other languages ,C/C++ supports a full complement of bitwise operators. Since C was designed to take the place of assembly language for most programming takes, it needed be able to support many operations that can be done in assembler, including operations refers to testing,
Setting ,or shifting the actual bits in a byte or word ,which correspond to the char and int data types and variants . You cannot use bit wise operations on float, double, long double ,void ,bool ,or other ,more complex types .Table 2-6 lists the operators that apply to bit wise operation .These operations are applied to the individual bits of the operations.

 Operators Action & AND | OR ^ Exclusive OR(XOR) ~ One’s complement(NOT) >> Shift right << Shft left

The Conditional C++ Operator

C/C++ contains a very powerful and convenient operator that replaces certain statements of the if-then-else from. The ternary operator (? 🙂 takes the general form
Exp1?Exp2:Exp3;
Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.The? operator works like this: Exp1 is evaluated. If it is true ,Exp2 is evaluated and becomes the value of the expression.If Exp1 is false,Exp3 is evaluated and its value becomes the value of the expression. For Example ,in
X=10;
Y=x>9?100:200;
Y is assigned the value 100. If x had been than 9, Y would have received the value 200.

Special C++ operators

C++ Supports some special operators that are used for specific purpose .

1. Sizeof : sizeof is a unary compile time operator that returns the length , in bytes ,of the variables or parenthesized type -specific that it precedes .For example ,assuming that integers are 4 bytes and doubles are 8 bytes ,
Double f;
Cout << sizeof(f);

The comma C++ operators

The comma operator strings together several expressions . The left side of the comma operator is always evaluated as void . This means that the expression on the right side becomes the value of the total comma separated expression. For example,
X=(y=3,y+1);
First assigns y the value 3 and then assigns x the value 4
(C) : : -scope resolution operator
(D): : -pointer-to-member declaratory
(E) ->* -pointer-to-member operator
(F).* -pointer-to-member operator
(G) delete – Memory release operator
(H) endl -Line feed operator
(I) new -Memory allocation operator
(J) setw -Field width operator

C++ Output operators

The only statement in the program is an output statement. The statement cout<<“C++ is better C.”;
causes the string in quotation marks to be displayed on the screen. This statement introduces two new C++ features, cout and <<.The identifier cout (pronounced as ‘C out’) is a predefined object that represents the slandered output stream in C++.Here, the slandered output stream represents the screen. It is also possible to redirect output to other devices .
The operator << is called the insertion or put to operator. It inserts(or sends) the comments of the variable on its right to the object on its left.
The object cout has a simple interface. If string represents a string variable, then the following statement will display its comments:
Cout << string;
You may recall that the operator << is the bit-wise left-shift operator and it can still be used for this purpose. This is an example of how an operator can be used for different purpose, depending on the context. This concept known as operator overloading, an important aspect of polymorphism.
It is important to note that we can still use printf() for displaying an output. C++ accepts this notation. However, we will use cout<< to maintain the spirit of C++.