C++ more operators: Mathematical , assignment , relational , Unary , typeid , ternary,etc
We have discussed logical and bitwise operator in another post here we will some more C++ operators.The mathematical,assignment,relational,Unary,typeid,ternary,etc are some other operators used heavily in C++.In this post we will discuss each of them in detail.
i)Mathematical operator
ii)Assignment operator
iii)Relational operator
iV)Unary operator
v)Typeid operator
vi)Ternary operator
i)Mathematical operators
All the operators which perform mathematical operation is included under this type.The operators included are:+(addition),-(subtraction),*(multiplication),%(modulus:this gives remainder and cannot be used with float value) and /(division).
We all are accustom to Mathematic subject so I am sure you will have no difficulty understanding the various mathematical operators.However,you may find modulus a bit nerve wrecking,so to make you understand what modulus is, consider that we divide 5 by 2 ,we know the remainder is 1.The modulus role is to provide us with the remainder whenever division is performed.Another example is shown below with the other operators implementation.
#include <iostream> using namespace std; int main( ) { signed int i1=2, i2=89 ; int addition=i1+i2 , subtraction=i1-i2 , division=i2/i1 , multiplication=i1*i2 , remainder=i2%i1 ; cout<< addition << endl << subtraction << endl << division << endl << multiplication << endl << remainder ; float f1=12.124 , f2=67.437 ,f3 ; /* f3=f2%f1 ; Error cannot use modulus with floating point type*/ f3=f2/f1 ; cout<< f3 ; cin.get() ; return 0 ; }
If you run the code you well get the remainder of each of the division performed and also the result gets truncated for ‘int’ type but not for ‘float’ type.This is a strict rule C++ stick follows at all cost because int type cannot represent a decimal point value whereas float type can.The other thing you will witnessed is modulus operation cannot be performed in floating point values.
ii)Assignment operators
The operator ‘=‘ is known as assignment operator.In every program if a value is assigned to a variable this operator is used and we have seen many program that use this operator for instance look at the code below.
int i=000000 ; ///Assignment operator is used.
So it’s sole purpose is to assign value to variable.
iii)Relational operators
Relational operators as the name suggest exhibit a relation between operands.These operators produce either true or false value.The relational operators are:<(smaller than) , >(greater than) , <=(less than or equal) , >=(greater than or equal) , ==(equivalent) and !=(not equivalent).
The operator ‘=’ and ‘==’ are very much different.The operator ‘==’ checks if the two operands value are same ,whereas the operator ‘=’ is use to assigned a value to another variable also known as assignment operator as seen above.
A program implementing various relational operators is shown below.
#include <iostream> using namespace std; int main( ) { int i1=23 , i2=768 ; float f1=23.65784 , f2=768.000 ; cout<< “i1<i2=” << (i1<i2) << ” ” << “i2>i1=” << (i2>i1) << endl ; cout<< “(i1<=f1)=” << (i1<=f1) << ” ” << “(f2>=i2)=” << (f2>=i2) << endl ; cout<< “(i1==f1)=” << (i1==f1) << ” ” << “(i2!=f2)=” << (i2!=f2) << endl ; cin.get( ) ; return 0 ; }
Check the output yourself.
iv)Unary operators
Unary operators require only one operand to operate hence the name unary(single) operator.Some of the Unary operators are:+,–,++(increment),—-(decrement),etc. some others might be !(negation),*(pointers discuss in Chapter 2),&(reference discuss in Chapter 2),new(memory allocation discuss in chapter 10l),delete(memory deallocation also discuss in chapter 10).
The sole purpose of ‘+’ and ‘–’ operators is to signify whether a value is +ve or -ve value.Say for example +89 or -67 .
The increment operator ‘++‘ increases the value of a variable by 1.The expression such as ++var1(consider ‘var1’ as a variable) is same as var1=var1+1;meaning the variable is added a value 1 and then assigned to itself.The decrement operator ‘–-‘ on the other hand decreases a variable value by 1 hence,the expression —-var1 is same as var1=var1-1.
When using increment or decrement operator you will come across expression like ++var1 or var1++.Do not get confuse here both the expression performs the same function which is to increment the value by 1.Same is the case for decrement —-var or var—- ,both are the same.
Ok above I have said ‘–var’ and ‘var–‘ are same but note ,in some specific case you will come across that they behave differently because they are made to do so.We will discuss more about such cases later on in another chapter.
The negation operator ‘!‘ mentioned here has the same meaning as the negation operator discuss in logical operator so visit the post here to understand it’s meaning.
Link :Logical operators
The unary operator ‘*‘ is not the same operator as the mathematical multiplication operator(*).It has different meaning and application,it’s uses is discuss in Chapter 2.
The operator ‘&‘ known as reference is also discuss in chapter 2.
The operators ‘new‘ and ‘delete‘ are discuss in Chapter 10!
The program below shows the uses of some of the unary operators we have discussed.
#include <iostream> using namespace std; int main( ) { int i, a=90 ; i=-a ; ///Unary negative cout<< i << endl ; i=i*(-a) ; ///the * here is multiplication operator///Decrement cout<< i << endl ; i=++i ; ///increment cout<< i << endl ; i=!a; cout<< i << endl ; cin.get() ; return 0 ;
Check the output,yourself,if you have trouble comment below.
v)Typeid operator
typeid() is an operator that can specify the type of a variable during run-time.To use this function you need to include the header’s file <typeinfo>.How to use this function is shown below.
#include <iostream> #include <typeinfo> using namespace std; int main( ) { int i=89 ; float f=908.890 ; cout<< typeid(i).name() << endl << typeid(f).name() << endl ; cin.get() ; return 0 ; }
You will get the output as
i
f
‘i’ means integer,’f’ means float.
vi)Ternary operator
This operator can return a different values for true and false case.It accept three operands and woks more or less like if() else(discuss in Chapter 2) statement.The expression using such operator is given below.
c=(a<b) ? 1 : 0 ;
The expression means if ‘a<b‘(‘a’ smaller than ‘b’) is true then ‘c’ is assigned ‘1’ else ‘c’ is assigned 0.A more complex use of ternary operator is shown below.
c=(a==b) ? b : (++b) ;
if ‘a==b'(‘a’ and ‘b’ value are same) is true then ‘c’ is assigned the value of ‘b’ but if not ‘c’ is assigned ‘++b’ and also note the value of b is incremented.A code example is given below.
int a =9,b=90,c; c=(a==b)? a : (++b); cout<< "c=" << c ;
Output,
c=91