# More operators: Mathematical , assignment , relational , Unary , typeid , ternary,etc

Mathematical,assignment,relational,Unary,typeid,ternary,etc are some other operators besides logical and bit wise operators used heavily in C++.In this post we will discuss each of them in detail.

#### 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).Since we are accustom to Mathematic subject I am sure you will have no difficulty here understanding the various 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 give 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 ;

subtraction=i1-i2 ,
division=i2/i1 ,
multiplication=i1*i2 ,
remainder=i2%i1 ;

<< 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 program and look at the division output the result gets truncated for int type but not for float type.This is a strict rule C++ stick to at all cost because int type cannot represent a decimal point value.

#### 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.

#### Relational operators

Relational operators as the name suggest provides a relation between the 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.
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 ;
}

Guess the output!

#### Unary operators

Unary operators require only one operand to operate so they are named unary 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 + and – operators purpose is to signify whether a value is +ve or -ve value.Say for example +89 or -67 .

The increment operator ‘++‘ when use with a variable increased it’s value by 1.So the expression ++var1 is same as var1=var1+1.The decrement operator ‘–‘ on the other hand decrease it’s value by 1 hence,the expression –var1 is same as var1=var1-1.When using increment or decrement operator they can be either attached in front of the variable or at the back of the variable i.e. ++var1 or var1++.The first form is known as “prefix” form and the second one is known as “postfix” increment form.In either of this usage the key meaning remains the same i.e. increased the value by 1,so var1++ is also same as var1=var1+1 .But as you cover more topics in C++ and get better in programing,in some specific case you will come across that they behave differently,yes of course they will because they are made to.I don’t want to trouble you now with the detail on why they behave differently so I’ll put off the explanation till Chapter 3.In Chapter 4 we will discuss on how and why they differ?.For now knowing that the two forms simply increment(for ++) and decrement(for –) the value will do the job.

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.

The unary operator ‘*‘ is not the same operator as the 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,you can visit the link here.

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 ;
}

#### Typeid operator

typeid() is an operator that can specify the type of a data during run-time(to know what is run-time visit this link).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 (means integer)
f (means float)

#### Ternary operator

This operator can return a different values for true and false case.This operator has 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) is true then c is assigned 1 else it is assigned 0 .A more complex use of ternary operator is shown below.

c=(a==b) ? b : (++b) ;

if a==b is true then c is assigned the value of b but if not c=(++b) and also the value of b is incremented.