SlideShare a Scribd company logo
1 of 112
Download to read offline
Mohammad Shaker 
mohammadshaker.com 
@ZGTRShaker 
2010, 11, 12, 13, 14 
C++ 
Programming Language 
L02 -CONVERSION+ENUM 
+OPERATORS
Type Conversion
Type Conversion 
•Used when different data types are used together 
–Expression 
•Arithmetic 
•Relational 
–Assignment operations 
–Parameter passage ( through functions ) 
–Return type ( From Value-Returning functions )
Type Conversion 
•Two types of Conversion: 
–Implicit 
•Promotion (widening, upcast ) 
–Promote a smaller type to a larger one 
»Thus, no loss of data 
–How to promote? 
»1st:Promotion 
»2nd: Arithmetic 
•Demotion 
–Demote a larger type to a smaller one 
»Thus, maybe loss of data! 
–Explicit
Type Conversion –Implicit promotion 
•How to promote? 
–bool, int, char, short, enum 
•Promoted to int 
•Integral values that can’t be represented as intare promoted to unsigned 
–if the expression still of a mixed type, the follow will be applied: 
Long double 
double 
float 
Unsigned long 
long 
unsigned 
int 
•Note: 
–When using “=” operator, the expression on the right will be converted to the type of the left
Type Conversion –Implicit promotion 
float x = 7; // 7 “int” is promoted to float 
inti1; 
char i2; 
float i3; 
i1 + i2 + i3 
// i1, i2 are promoted to float 
3.4 + ‘b’// b “char” is promoted to double 
3.43265L + ‘b’// b “int” is promoted to Long double 
inti1; 
inti2; 
double i3; 
i1 + i2 + i3 
// i1, i2 are promoted to double 
inti1; 
double i3; 
i1 + ‘N’ + i3 
// i1, N are promoted to double 
intx = 7.4; // 7 “double” is demoted to int= 7 loss of data!
Type Conversion –Explicit promotion 
float x = float(7); // becomes 7.0 
float x = (float)7;// becomes 7.0 
int(7.6)// becomes 7 
(int) 7.6// becomes 7 
float (7/4) = 1.0 
7/4 = 1 
float(7) / float(4) = 1.75 
double(7) / double(4) = 1.75
Type Conversion –Explicit Casting 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
charc; 
inti; 
i= c;// implicit casting 
cout<< i<< endl; 
} 
0 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
charcece= 'c'; 
inti; 
i= cece; // implicit casting 
cout<< i<< endl; 
} 
99
Type Conversion –Explicit Casting 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
charcece= 'c'; 
inti= cece; // implicit casting 
cout<< i<< endl; 
} 
99 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
charcece= 'c'; 
inti; 
i= int(cece); // explicit casting 
cout<< i<< endl; 
} 
99
Type Conversion –Explicit Casting 
#include <iostream> 
using namespace::std; 
void main() 
{ 
char c = 'c'; 
inti; 
i= int(c -'d');// explicit casting 
cout<< i<< endl; 
} 
-1 
#include <iostream> 
using namespace::std; 
void main() 
{ 
char c = 'c'; 
inti; 
i= int(c -'c');// explicit casting 
cout<< i<< endl; 
} 
0
Type Conversion –Explicit Casting 
#include <iostream> 
using namespace::std; 
void main() 
{ 
char c = 'c'; 
inti; 
i= int(c -‘2');// explicit casting 
cout<< i<< endl; 
} 
49 
#include <iostream> 
using namespace::std; 
void main() 
{ 
char c = 'c'; 
inti; 
i= int(c -‘0');// explicit casting 
cout<< i<< endl; 
} 
51
Type Conversion –Explicit Casting 
•Types of explicating Casting 
–static_cast 
–dynamic_cast 
–const_cast 
–reinterpret_cast
Explicit Casting –static_castAt compile time! Thus, Type & object should be fully known at compile time
Explicit Casting –static_cast 
•We were used to do that 
•Now, we can do that (Not necessary, just highlighting the cast) 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
inti = 25;longl; floatf; 
l = i; 
f = i; 
} 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
inti = 25; longl; floatf; 
l = static_cast<long>(i1); 
f = static_cast<float>(i1); 
}
Type Conversion –Explicit Casting 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
inti1 = 25; 
longi2; 
floati3; 
i1 = i2; 
i1 = i3; 
i2 = i3; 
} 
Compile & Run (with warnings) 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
inti1 = 25; 
longi2; 
floati3; 
i1 = static_cast<long>(i2); 
i1 = static_cast<float>(i3); 
i2 = static_cast<long>(i3); 
} 
Compile & Run (No warnings since we used static_cast)
enum
Float, double, long double 
C++ data types 
Structured 
Simple 
Address 
Pointer 
Reference 
enum 
Floating 
Array 
Struct 
Union 
Class 
Char, Short, int, long, yool 
Integral
enum 
•Intro 
–Deal with limited range of constants (set of constants) 
•Set of colors 
–Used in place of the actual integer values 
–C++ allows creation of a new simple type by listing (enumerating) all the ordered values in the domain of a new type 
•Syntax: 
enumTypeName{ value1, value2, value3, };
enum 
•Declaring variables from the “enumerated” type 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars {Nissan = 1, BMW,Mercedes,Ferrari,Renault}; 
Cars MyCar, YourCar; 
} 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars { Nissan = 1, BMW, Mercedes, Ferrari, Renault } MyCar, YourCar; 
}
enum 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars { Nissan, Nissan, Mercedes, Ferrari,Renault}; 
} 
Compiler error. Values must be UNIQUE
enum 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars { Nissan = 1, BMW, Mercedes, Ferrari,Renault}; 
} 
Nissan < BMW < Mercedes < 
The Values are ordered from 1upward 
Nissan = 1 
BMW = 2 
Mercedes = 3 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars { Nissan, BMW, Mercedes, Ferrari,Renault}; 
} 
Nissan < BMW < Mercedes < 
The defaultValues are ordered from 0 upward 
Nissan = 0 
BMW = 1 
Mercedes = 2
enum 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars {Nissan = 1, BMW, Mercedes = 7, Ferrari, Renault }; 
cout<< Renault; 
} 
9 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars { Nissan = 1, BMW, Mercedes = 5, Ferrari,Renault}; 
} 
The Values are incremented by 1 
Nissan = 1, BMW = 2 
Mercedes = 5, Ferrari = 6, Renault = 7
enum 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars { Nissan = 1, BMW, Mercedes = BMW + 4, Ferrari,Renault= Renault + 2}; 
} 
Complier error Renault undeclared identifier 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars { Nissan = 1, BMW, Mercedes = BMW + 4, Ferrari,Renault= Ferrari + 2}; 
} 
The Values are incremented by 1upward 
Nissan = 1, BMW = 2 
Mercedes = 6, Ferrari = 7, Renault = 9
enum 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars { Nissan = 1, BMW, Mercedes = 1, Ferrari,Renault}; 
} 
The values are ordered from 1upward 
Nissan = 1, BMW = 2 
Mercedes = 1, Ferrari = 2, Renault = 3
enum 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
// Declare MyCarof type Cars and initialize it to Nissan 
enumCars{ Nissan,BMW,Mercedes,Ferrari,Renault} MyCar= Nissan; 
} 
Compile and run 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars { Nissan = 1, BMW, Mercedes,Ferrari, Renault }; 
Cars MyCar, YourCar; 
cout<< MyCar; 
} 
0
enum 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars {Nissan=1,BMW,Mercedes,Ferrari,Renault} MyCar=Nissan; 
MyCar= Renault;// legal 
inti1 = BMW, i2 = Mercedes;// legal 
i2 = Ferrari;// legal 
cout<< "i2 = "<< i2 << endl; 
} 
i2 = 4
enum 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars {Nissan=1,BMW,Mercedes,Ferrari,Renault}MyCar=Nissan; 
MyCar= Renault; 
inti1 = BMW; 
i1 = MyCar; 
} 
i1 = MyCar; //legal 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars {Nissan=1,BMW,Mercedes,Ferrari,Renault}MyCar=Nissan; 
MyCar= Renault; 
inti1 = BMW; 
MyCar= i1; 
} 
Compiler error 
MyCar= i1; //illegal 
Can’t put an integer into an enumtype. guess why?
enum 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars {Nissan,BMW,Mercedes,Ferrari,Renault}; 
Cars Mine, Yours; 
Mine = BMW; 
Yours = Mine; 
cout<< Yours << endl; 
} 
1 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars { Nissan, BMW,Mercedes,Ferrari,Renault}; 
Cars Mine, Yours; 
Mine = BMW; 
Mine = Yours; 
cout<< Mine << endl; 
} 
0
enum 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars {Nissan, BMW,Mercedes,Ferrari,Renault}; 
Cars Mine, Yours; 
Mine = BMW; 
Yours = Mine++; 
cout<< Yours << endl; 
} 
Compiler error 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars {Nissan,BMW,Mercedes,Ferrari,Renault}; 
Cars Mine, Yours; 
Mine = BMW; 
Yours = Mine + 1; 
cout<< Yours << endl; 
} 
Compiler error
Operators
Operators 
•Assignment 
–b = 5;// a = 5 
–a = 3 + (b = 6 ); // a = 8 
–a = b = 5;// legal a = b = 5 
•Equality 
–2 == 2 // true 
–2 == 3 // false 
•Arithmetic Operation 
–+, -, *, /, %
Operators 
•Division / 
–The result of division depends on the type of the two operands 
•If one of the operands is float 
–The result is float typed 
•Otherwise 
–The result is integer typed 
–Examples: 
•13 / 4 = 3 
•13.0 / 4 = 3.25 
•13 / 4.0 = 3.25 
•13.0 / 4.0 = 3.25 
•Mod % 
–Applied to integer types only 
–Operands: 
•Both positive 
–Result is positive 
•One or both negative 
–Result is machine-dependent
Operators 
11.3 % 4; 
Compiler error, left side is a double 
22 % 7; 
1 
21 % 7; 
0 
2%6; 
2 
23%-6; 
5 
-23%-6; 
-5 
2%-6; 
2
Operators Rules of Precedence
Operators 
•Rules of precedence: 
–1st: () 
•When multi nested () 
–Inners come first 
–2nd: *, /, % 
•Left to right 
–3rd: +, - 
•Left to right
Operators
Operators 
voidmain() 
{ 
cout<< (9*2) * 3 / 2 / 2 + (3+2) * (1-10 ) << endl; 
} 
voidmain() 
{ 
cout<< 9*2 * 3 / 2 / 2 + (3+2) * (1-10 ) << endl; 
} 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
cout<< 3/2 << endl; 
} 
voidmain() 
{ 
cout<<(float)3/2<<endl; 
system("pause"); 
} 
1 (int) 
1.5 
-32 
-32
Operators 
voidmain() 
{ 
cout<< (9*2) * 3 / 2 /* 2 + (3+2) * (1-10 ) << endl; 
} 
voidmain() 
{ 
intc = 1; 
c = 1 + 2; 
cout<< c << endl; 
c += 2; 
cout<< c << endl; 
} 
voidmain() 
{ 
intc = 1; 
c = 1 + 2; 
cout<< c << endl; 
c = 1; 
c += 2; 
cout<< c << endl; 
} 
3 
3 
Illegal indirection /* compiler error 
3 
5
Operators 
•Special Case: 
–When incrementing “+” or decrementing “-” by 1 
•All are the same: 
b = b + 1; 
b += 1; 
b++;
Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1; 
c++; 
c--; 
cout<< c << endl; 
} 
1 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1; 
++c; 
cout<< c << endl; 
} 
2
Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1; 
--c; 
cout<< c << endl; 
} 
0 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc; 
c = 1; 
cout<< c++<< endl; 
} 
1
Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a; 
a = ++c;// a = 2, c = 2 
c--; 
cout<< a << endl; 
} 
2 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
inti=0; 
++i=2; 
cout<<i<<endl; 
system("pause"); 
} 
2
Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
inti=0; 
i++=2; 
cout<<i<<endl; 
system("pause"); 
} 
Compiler error
Relational Operators
Relational Operators
Relational Operators 
voidmain() 
{ 
intc=1, a=0; 
if(c==2) 
{ 
c=9; 
} 
cout<<c<<endl; 
system("pause"); 
} 
1 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc=1, a=0; 
if(c++==2) 
{ 
c=9; 
} 
cout<<c<<endl; 
system("pause"); 
} 
2
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc=1, a=0; 
if(++c==2) 
{ 
c=9; 
} 
cout<<c<<endl; 
system("pause"); 
} 
9 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc=1, a=0; 
if(c=2) 
{ 
c=9; 
} 
cout<<c<<endl; 
system("pause"); 
} 
9
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc=1, a=0; 
if(c=2) 
{ 
c=9; 
} 
cout<<c<<endl; 
system("pause"); 
} 
9 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc=1, a=0; 
a=c++; 
cout<<a<<endl<<c<<endl; 
system("pause"); 
} 
1 
2
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc=1, a=0; 
a=++c; 
cout<<a<<endl<<c<<endl; 
system("pause"); 
} 
2 
2
Relational Operators 
c++ 
++c 
Introduce a L-VALUE nota variable 
Introduce a VARIABLE nota value or L-Value
Relational Operators 
c++ 
++c 
Introduce a L-VALUE nota variable 
Introduce a VARIABLE nota value or L-Value 
Can be assigned! 
Can’t be assigned!
Relational Operators 
c++ 
Introduce a L-VALUE nota variable 
•What is l-VALUE? 
Expressions that refer to memory locations are called "l-value" expressions. An l-value represents a storage region's "locator" value, or a "left" value, implying that it can appear on the left of the equal sign (=). L-values are often identifiers.
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc=1, a=0; 
c++= a; 
cout<<a<<endl<<c<<endl; 
system("pause"); 
} 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc=1, a=0; 
++c = a; 
cout<<a<<endl<<c<<endl; 
system("pause"); 
} 
Compiler error! 
0 
0 
Variable can be assigned! 
l-Value can’t be assigned!
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++> 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
2 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++>= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
2
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(++c > 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
2 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(++c >= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(++c = 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error 
l-Value must not be assigned
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error 
l-Value must not be assigned
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error 
l-Value must not be assigned
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error 
Let’s roll back to here
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error 
Can’t be calculated now!
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error 
Can’t be calculated now!
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error 
Can’t be calculated now!
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error 
Can’t be calculated now!
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error 
Can’t be calculated now!
Relational Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(c++= 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error 
Can’t be calculated now!
Relational Operators
Relational Operators with strings 
•Comparing with collating sequence 
•If the strings have different lenghts 
–The shorter one is smaller that the larger 
str1 
= “Hello” 
str2 
= “Hi” 
str3 
= “Air” 
str4 
= “Bill”
Logical Operators
Logical Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if((a == 0 ) || (c == 3)) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9
Logical Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if(!(a) > 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
1 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if((a == 2 ) && (c == 3)) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9
Logical Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a; 
if((a == 0 ) && (c == 1)) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a; 
if((a == 0 ) && (c == 2)) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
1
Logical Operators 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a; 
if(a == 0 ) && (c == 2) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
Compiler error 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 0, a; 
if(( 0 < c ) && ( c <= 12 )) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
0
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Logical Operators 
•Not always behave as expected 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = -1, a; 
if( 0 < c <= 12 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
// Always evaluated true!
Conditional Operator? 
•Condition? Result1: Result2 
•Condition 
–If True 
•Result1 
–Else // condition is false 
•Result 2
Conditional Operator? 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc; 
c ==0? c = 2: c = 1; 
cout<< c << endl; 
} 
2 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc; 
c ==2? c = 2: c = 1; 
cout<< c << endl; 
} 
1
Conditional Operator? 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc; 
c!= 0? c = 2: c = 1; 
cout<< c << endl; 
} 
1 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc; 
c ==0? 2: 1; 
cout<< c << endl; 
} 
0
Conditional Operator? 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc; 
c!= 0? 2: 1; 
cout<< c << endl; 
} 
0 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc; 
c!= 0? c=2: 1; 
cout<< c << endl; 
} 
0
Conditional Operator? 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc; 
c = 0? c=2: 1; 
cout<< c << endl; 
} 
1 
(c = 0) Simply the variable c is assigned 0 
If(zero) returns false
Let’s Crack Some Code
Code Cracking
Code Cracking 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars {Nissan,BMW,Mercedes, Ferrari,Renault=Nissan--}; Cars MyCar= Renault; 
} 
Compiler error Nissan-- 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars{Nissan,BMW,Mercedes,Ferrari,Renault++}MyCar=Nissan; 
} 
Compiler error Renault++, why?
Code Cracking 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
enumCars { Nissan = 2.4,BMW,Mercedes,Ferrari,Renault }MyCar= Nissan; 
MyCar= Renault; 
inti1 = MyCar; 
} 
Compiler error Nissan = 2.4 must be integer 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a; 
if((a = 0 ) && (c = 2)) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9
Code Cracking 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc = 1, a = 0; 
if((a = 3) > 2 ) 
{ 
c = 9; 
} 
cout<< c << endl; 
} 
9 
#include<iostream> 
usingnamespace::std; 
constx = 20; 
voidmain() 
{ 
} 
2008 Compiler: Compiler error no default intsupported 
2005 Compiler: Compile & Run Intassumed
Code Cracking 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc; 
if(c++== 1) 
{ 
cout<< "joo"<<endl; 
} 
else 
{ 
cout<< "yoo"<< endl; 
} 
} 
yoo 
#include<iostream> 
usingnamespace::std; 
voidmain() 
{ 
intc; 
if(++c == 1) 
{ 
cout<< "joo"<<endl; 
} 
else 
{ 
cout<< " "<< endl; 
} 
} 
joo

More Related Content

What's hot

What's hot (20)

C++ L10-Inheritance
C++ L10-InheritanceC++ L10-Inheritance
C++ L10-Inheritance
 
C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
 
c programming
c programmingc programming
c programming
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
c programming
c programmingc programming
c programming
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 
C tech questions
C tech questionsC tech questions
C tech questions
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Modern C++ Concurrency API
Modern C++ Concurrency APIModern C++ Concurrency API
Modern C++ Concurrency API
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
 
Functional C++
Functional C++Functional C++
Functional C++
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit Testing
 
06.Loops
06.Loops06.Loops
06.Loops
 

Viewers also liked

Java script final presentation
Java script final presentationJava script final presentation
Java script final presentation
Adhoura Academy
 

Viewers also liked (18)

Java script
Java scriptJava script
Java script
 
Inside jQuery (2011)
Inside jQuery (2011)Inside jQuery (2011)
Inside jQuery (2011)
 
JSON - Quick Overview
JSON - Quick OverviewJSON - Quick Overview
JSON - Quick Overview
 
Java Script Object Notation (JSON)
Java Script Object Notation (JSON)Java Script Object Notation (JSON)
Java Script Object Notation (JSON)
 
Fundamental JQuery
Fundamental JQueryFundamental JQuery
Fundamental JQuery
 
Introduction to JSON
Introduction to JSONIntroduction to JSON
Introduction to JSON
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
HTML/CSS/java Script/Jquery
HTML/CSS/java Script/JqueryHTML/CSS/java Script/Jquery
HTML/CSS/java Script/Jquery
 
A quick guide to Css and java script
A quick guide to Css and  java scriptA quick guide to Css and  java script
A quick guide to Css and java script
 
Java script
Java scriptJava script
Java script
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
Java script
Java scriptJava script
Java script
 
Java script final presentation
Java script final presentationJava script final presentation
Java script final presentation
 
Java script
Java scriptJava script
Java script
 
Java script
Java scriptJava script
Java script
 
An Overview of HTML, CSS & Java Script
An Overview of HTML, CSS & Java ScriptAn Overview of HTML, CSS & Java Script
An Overview of HTML, CSS & Java Script
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 

Similar to C++ L02-Conversion+enum+Operators

c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2
sajidpk92
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
aeden_brines
 
I need help to modify my code according to the instructions- Modify th.pdf
I need help to modify my code according to the instructions- Modify th.pdfI need help to modify my code according to the instructions- Modify th.pdf
I need help to modify my code according to the instructions- Modify th.pdf
pnaran46
 

Similar to C++ L02-Conversion+enum+Operators (20)

Hello world! Intro to C++
Hello world! Intro to C++Hello world! Intro to C++
Hello world! Intro to C++
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
 
c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languages
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
lecture56.ppt
lecture56.pptlecture56.ppt
lecture56.ppt
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
C++
C++C++
C++
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
 
MIPS_Programming.pdf
MIPS_Programming.pdfMIPS_Programming.pdf
MIPS_Programming.pdf
 
Cbasic
CbasicCbasic
Cbasic
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
 
C++ programming basics
C++ programming basicsC++ programming basics
C++ programming basics
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
 
I need help to modify my code according to the instructions- Modify th.pdf
I need help to modify my code according to the instructions- Modify th.pdfI need help to modify my code according to the instructions- Modify th.pdf
I need help to modify my code according to the instructions- Modify th.pdf
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 

More from Mohammad Shaker

More from Mohammad Shaker (20)

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
 

Recently uploaded

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 

Recently uploaded (20)

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 

C++ L02-Conversion+enum+Operators

  • 1. Mohammad Shaker mohammadshaker.com @ZGTRShaker 2010, 11, 12, 13, 14 C++ Programming Language L02 -CONVERSION+ENUM +OPERATORS
  • 3. Type Conversion •Used when different data types are used together –Expression •Arithmetic •Relational –Assignment operations –Parameter passage ( through functions ) –Return type ( From Value-Returning functions )
  • 4. Type Conversion •Two types of Conversion: –Implicit •Promotion (widening, upcast ) –Promote a smaller type to a larger one »Thus, no loss of data –How to promote? »1st:Promotion »2nd: Arithmetic •Demotion –Demote a larger type to a smaller one »Thus, maybe loss of data! –Explicit
  • 5. Type Conversion –Implicit promotion •How to promote? –bool, int, char, short, enum •Promoted to int •Integral values that can’t be represented as intare promoted to unsigned –if the expression still of a mixed type, the follow will be applied: Long double double float Unsigned long long unsigned int •Note: –When using “=” operator, the expression on the right will be converted to the type of the left
  • 6. Type Conversion –Implicit promotion float x = 7; // 7 “int” is promoted to float inti1; char i2; float i3; i1 + i2 + i3 // i1, i2 are promoted to float 3.4 + ‘b’// b “char” is promoted to double 3.43265L + ‘b’// b “int” is promoted to Long double inti1; inti2; double i3; i1 + i2 + i3 // i1, i2 are promoted to double inti1; double i3; i1 + ‘N’ + i3 // i1, N are promoted to double intx = 7.4; // 7 “double” is demoted to int= 7 loss of data!
  • 7. Type Conversion –Explicit promotion float x = float(7); // becomes 7.0 float x = (float)7;// becomes 7.0 int(7.6)// becomes 7 (int) 7.6// becomes 7 float (7/4) = 1.0 7/4 = 1 float(7) / float(4) = 1.75 double(7) / double(4) = 1.75
  • 8. Type Conversion –Explicit Casting #include<iostream> usingnamespace::std; voidmain() { charc; inti; i= c;// implicit casting cout<< i<< endl; } 0 #include<iostream> usingnamespace::std; voidmain() { charcece= 'c'; inti; i= cece; // implicit casting cout<< i<< endl; } 99
  • 9. Type Conversion –Explicit Casting #include<iostream> usingnamespace::std; voidmain() { charcece= 'c'; inti= cece; // implicit casting cout<< i<< endl; } 99 #include<iostream> usingnamespace::std; voidmain() { charcece= 'c'; inti; i= int(cece); // explicit casting cout<< i<< endl; } 99
  • 10. Type Conversion –Explicit Casting #include <iostream> using namespace::std; void main() { char c = 'c'; inti; i= int(c -'d');// explicit casting cout<< i<< endl; } -1 #include <iostream> using namespace::std; void main() { char c = 'c'; inti; i= int(c -'c');// explicit casting cout<< i<< endl; } 0
  • 11. Type Conversion –Explicit Casting #include <iostream> using namespace::std; void main() { char c = 'c'; inti; i= int(c -‘2');// explicit casting cout<< i<< endl; } 49 #include <iostream> using namespace::std; void main() { char c = 'c'; inti; i= int(c -‘0');// explicit casting cout<< i<< endl; } 51
  • 12. Type Conversion –Explicit Casting •Types of explicating Casting –static_cast –dynamic_cast –const_cast –reinterpret_cast
  • 13. Explicit Casting –static_castAt compile time! Thus, Type & object should be fully known at compile time
  • 14. Explicit Casting –static_cast •We were used to do that •Now, we can do that (Not necessary, just highlighting the cast) #include<iostream> usingnamespace::std; voidmain() { inti = 25;longl; floatf; l = i; f = i; } #include<iostream> usingnamespace::std; voidmain() { inti = 25; longl; floatf; l = static_cast<long>(i1); f = static_cast<float>(i1); }
  • 15. Type Conversion –Explicit Casting #include<iostream> usingnamespace::std; voidmain() { inti1 = 25; longi2; floati3; i1 = i2; i1 = i3; i2 = i3; } Compile & Run (with warnings) #include<iostream> usingnamespace::std; voidmain() { inti1 = 25; longi2; floati3; i1 = static_cast<long>(i2); i1 = static_cast<float>(i3); i2 = static_cast<long>(i3); } Compile & Run (No warnings since we used static_cast)
  • 16. enum
  • 17. Float, double, long double C++ data types Structured Simple Address Pointer Reference enum Floating Array Struct Union Class Char, Short, int, long, yool Integral
  • 18. enum •Intro –Deal with limited range of constants (set of constants) •Set of colors –Used in place of the actual integer values –C++ allows creation of a new simple type by listing (enumerating) all the ordered values in the domain of a new type •Syntax: enumTypeName{ value1, value2, value3, };
  • 19. enum •Declaring variables from the “enumerated” type #include<iostream> usingnamespace::std; voidmain() { enumCars {Nissan = 1, BMW,Mercedes,Ferrari,Renault}; Cars MyCar, YourCar; } #include<iostream> usingnamespace::std; voidmain() { enumCars { Nissan = 1, BMW, Mercedes, Ferrari, Renault } MyCar, YourCar; }
  • 20. enum #include<iostream> usingnamespace::std; voidmain() { enumCars { Nissan, Nissan, Mercedes, Ferrari,Renault}; } Compiler error. Values must be UNIQUE
  • 21. enum #include<iostream> usingnamespace::std; voidmain() { enumCars { Nissan = 1, BMW, Mercedes, Ferrari,Renault}; } Nissan < BMW < Mercedes < The Values are ordered from 1upward Nissan = 1 BMW = 2 Mercedes = 3 #include<iostream> usingnamespace::std; voidmain() { enumCars { Nissan, BMW, Mercedes, Ferrari,Renault}; } Nissan < BMW < Mercedes < The defaultValues are ordered from 0 upward Nissan = 0 BMW = 1 Mercedes = 2
  • 22. enum #include<iostream> usingnamespace::std; voidmain() { enumCars {Nissan = 1, BMW, Mercedes = 7, Ferrari, Renault }; cout<< Renault; } 9 #include<iostream> usingnamespace::std; voidmain() { enumCars { Nissan = 1, BMW, Mercedes = 5, Ferrari,Renault}; } The Values are incremented by 1 Nissan = 1, BMW = 2 Mercedes = 5, Ferrari = 6, Renault = 7
  • 23. enum #include<iostream> usingnamespace::std; voidmain() { enumCars { Nissan = 1, BMW, Mercedes = BMW + 4, Ferrari,Renault= Renault + 2}; } Complier error Renault undeclared identifier #include<iostream> usingnamespace::std; voidmain() { enumCars { Nissan = 1, BMW, Mercedes = BMW + 4, Ferrari,Renault= Ferrari + 2}; } The Values are incremented by 1upward Nissan = 1, BMW = 2 Mercedes = 6, Ferrari = 7, Renault = 9
  • 24. enum #include<iostream> usingnamespace::std; voidmain() { enumCars { Nissan = 1, BMW, Mercedes = 1, Ferrari,Renault}; } The values are ordered from 1upward Nissan = 1, BMW = 2 Mercedes = 1, Ferrari = 2, Renault = 3
  • 25. enum #include<iostream> usingnamespace::std; voidmain() { // Declare MyCarof type Cars and initialize it to Nissan enumCars{ Nissan,BMW,Mercedes,Ferrari,Renault} MyCar= Nissan; } Compile and run #include<iostream> usingnamespace::std; voidmain() { enumCars { Nissan = 1, BMW, Mercedes,Ferrari, Renault }; Cars MyCar, YourCar; cout<< MyCar; } 0
  • 26. enum #include<iostream> usingnamespace::std; voidmain() { enumCars {Nissan=1,BMW,Mercedes,Ferrari,Renault} MyCar=Nissan; MyCar= Renault;// legal inti1 = BMW, i2 = Mercedes;// legal i2 = Ferrari;// legal cout<< "i2 = "<< i2 << endl; } i2 = 4
  • 27. enum #include<iostream> usingnamespace::std; voidmain() { enumCars {Nissan=1,BMW,Mercedes,Ferrari,Renault}MyCar=Nissan; MyCar= Renault; inti1 = BMW; i1 = MyCar; } i1 = MyCar; //legal #include<iostream> usingnamespace::std; voidmain() { enumCars {Nissan=1,BMW,Mercedes,Ferrari,Renault}MyCar=Nissan; MyCar= Renault; inti1 = BMW; MyCar= i1; } Compiler error MyCar= i1; //illegal Can’t put an integer into an enumtype. guess why?
  • 28. enum #include<iostream> usingnamespace::std; voidmain() { enumCars {Nissan,BMW,Mercedes,Ferrari,Renault}; Cars Mine, Yours; Mine = BMW; Yours = Mine; cout<< Yours << endl; } 1 #include<iostream> usingnamespace::std; voidmain() { enumCars { Nissan, BMW,Mercedes,Ferrari,Renault}; Cars Mine, Yours; Mine = BMW; Mine = Yours; cout<< Mine << endl; } 0
  • 29. enum #include<iostream> usingnamespace::std; voidmain() { enumCars {Nissan, BMW,Mercedes,Ferrari,Renault}; Cars Mine, Yours; Mine = BMW; Yours = Mine++; cout<< Yours << endl; } Compiler error #include<iostream> usingnamespace::std; voidmain() { enumCars {Nissan,BMW,Mercedes,Ferrari,Renault}; Cars Mine, Yours; Mine = BMW; Yours = Mine + 1; cout<< Yours << endl; } Compiler error
  • 31. Operators •Assignment –b = 5;// a = 5 –a = 3 + (b = 6 ); // a = 8 –a = b = 5;// legal a = b = 5 •Equality –2 == 2 // true –2 == 3 // false •Arithmetic Operation –+, -, *, /, %
  • 32. Operators •Division / –The result of division depends on the type of the two operands •If one of the operands is float –The result is float typed •Otherwise –The result is integer typed –Examples: •13 / 4 = 3 •13.0 / 4 = 3.25 •13 / 4.0 = 3.25 •13.0 / 4.0 = 3.25 •Mod % –Applied to integer types only –Operands: •Both positive –Result is positive •One or both negative –Result is machine-dependent
  • 33. Operators 11.3 % 4; Compiler error, left side is a double 22 % 7; 1 21 % 7; 0 2%6; 2 23%-6; 5 -23%-6; -5 2%-6; 2
  • 34. Operators Rules of Precedence
  • 35. Operators •Rules of precedence: –1st: () •When multi nested () –Inners come first –2nd: *, /, % •Left to right –3rd: +, - •Left to right
  • 37. Operators voidmain() { cout<< (9*2) * 3 / 2 / 2 + (3+2) * (1-10 ) << endl; } voidmain() { cout<< 9*2 * 3 / 2 / 2 + (3+2) * (1-10 ) << endl; } #include<iostream> usingnamespace::std; voidmain() { cout<< 3/2 << endl; } voidmain() { cout<<(float)3/2<<endl; system("pause"); } 1 (int) 1.5 -32 -32
  • 38. Operators voidmain() { cout<< (9*2) * 3 / 2 /* 2 + (3+2) * (1-10 ) << endl; } voidmain() { intc = 1; c = 1 + 2; cout<< c << endl; c += 2; cout<< c << endl; } voidmain() { intc = 1; c = 1 + 2; cout<< c << endl; c = 1; c += 2; cout<< c << endl; } 3 3 Illegal indirection /* compiler error 3 5
  • 39. Operators •Special Case: –When incrementing “+” or decrementing “-” by 1 •All are the same: b = b + 1; b += 1; b++;
  • 40. Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1; c++; c--; cout<< c << endl; } 1 #include<iostream> usingnamespace::std; voidmain() { intc = 1; ++c; cout<< c << endl; } 2
  • 41. Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1; --c; cout<< c << endl; } 0 #include<iostream> usingnamespace::std; voidmain() { intc; c = 1; cout<< c++<< endl; } 1
  • 42. Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a; a = ++c;// a = 2, c = 2 c--; cout<< a << endl; } 2 #include<iostream> usingnamespace::std; voidmain() { inti=0; ++i=2; cout<<i<<endl; system("pause"); } 2
  • 43. Operators #include<iostream> usingnamespace::std; voidmain() { inti=0; i++=2; cout<<i<<endl; system("pause"); } Compiler error
  • 46. Relational Operators voidmain() { intc=1, a=0; if(c==2) { c=9; } cout<<c<<endl; system("pause"); } 1 #include<iostream> usingnamespace::std; voidmain() { intc=1, a=0; if(c++==2) { c=9; } cout<<c<<endl; system("pause"); } 2
  • 47. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc=1, a=0; if(++c==2) { c=9; } cout<<c<<endl; system("pause"); } 9 #include<iostream> usingnamespace::std; voidmain() { intc=1, a=0; if(c=2) { c=9; } cout<<c<<endl; system("pause"); } 9
  • 48. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc=1, a=0; if(c=2) { c=9; } cout<<c<<endl; system("pause"); } 9 #include<iostream> usingnamespace::std; voidmain() { intc=1, a=0; a=c++; cout<<a<<endl<<c<<endl; system("pause"); } 1 2
  • 49. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc=1, a=0; a=++c; cout<<a<<endl<<c<<endl; system("pause"); } 2 2
  • 50. Relational Operators c++ ++c Introduce a L-VALUE nota variable Introduce a VARIABLE nota value or L-Value
  • 51. Relational Operators c++ ++c Introduce a L-VALUE nota variable Introduce a VARIABLE nota value or L-Value Can be assigned! Can’t be assigned!
  • 52. Relational Operators c++ Introduce a L-VALUE nota variable •What is l-VALUE? Expressions that refer to memory locations are called "l-value" expressions. An l-value represents a storage region's "locator" value, or a "left" value, implying that it can appear on the left of the equal sign (=). L-values are often identifiers.
  • 53. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc=1, a=0; c++= a; cout<<a<<endl<<c<<endl; system("pause"); } #include<iostream> usingnamespace::std; voidmain() { intc=1, a=0; ++c = a; cout<<a<<endl<<c<<endl; system("pause"); } Compiler error! 0 0 Variable can be assigned! l-Value can’t be assigned!
  • 54. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++> 2 ) { c = 9; } cout<< c << endl; } 2 #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++>= 2 ) { c = 9; } cout<< c << endl; } 2
  • 55. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(++c > 2 ) { c = 9; } cout<< c << endl; } 2 #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(++c >= 2 ) { c = 9; } cout<< c << endl; } 9
  • 56. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(++c = 2 ) { c = 9; } cout<< c << endl; } 9
  • 57. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2) { c = 9; } cout<< c << endl; } Compiler error
  • 58. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error
  • 59. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error
  • 60. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error
  • 61. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error
  • 62. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error
  • 63. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error l-Value must not be assigned
  • 64. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error l-Value must not be assigned
  • 65. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error l-Value must not be assigned
  • 66. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error Let’s roll back to here
  • 67. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error Can’t be calculated now!
  • 68. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error Can’t be calculated now!
  • 69. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error Can’t be calculated now!
  • 70. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error Can’t be calculated now!
  • 71. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error Can’t be calculated now!
  • 72. Relational Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(c++= 2 ) { c = 9; } cout<< c << endl; } Compiler error Can’t be calculated now!
  • 74. Relational Operators with strings •Comparing with collating sequence •If the strings have different lenghts –The shorter one is smaller that the larger str1 = “Hello” str2 = “Hi” str3 = “Air” str4 = “Bill”
  • 76. Logical Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if((a == 0 ) || (c == 3)) { c = 9; } cout<< c << endl; } 9
  • 77. Logical Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if(!(a) > 2 ) { c = 9; } cout<< c << endl; } 1 #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if((a == 2 ) && (c == 3)) { c = 9; } cout<< c << endl; } 9
  • 78. Logical Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a; if((a == 0 ) && (c == 1)) { c = 9; } cout<< c << endl; } 9 #include<iostream> usingnamespace::std; voidmain() { intc = 1, a; if((a == 0 ) && (c == 2)) { c = 9; } cout<< c << endl; } 1
  • 79. Logical Operators #include<iostream> usingnamespace::std; voidmain() { intc = 1, a; if(a == 0 ) && (c == 2) { c = 9; } cout<< c << endl; } Compiler error #include<iostream> usingnamespace::std; voidmain() { intc = 0, a; if(( 0 < c ) && ( c <= 12 )) { c = 9; } cout<< c << endl; } 0
  • 80. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 81. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 82. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 83. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 84. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 85. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 86. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 87. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 88. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 89. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 90. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 91. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 92. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 93. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 94. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 95. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 96. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 97. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 98. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 99. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 100. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 101. Logical Operators •Not always behave as expected #include<iostream> usingnamespace::std; voidmain() { intc = -1, a; if( 0 < c <= 12 ) { c = 9; } cout<< c << endl; } 9 // Always evaluated true!
  • 102. Conditional Operator? •Condition? Result1: Result2 •Condition –If True •Result1 –Else // condition is false •Result 2
  • 103. Conditional Operator? #include<iostream> usingnamespace::std; voidmain() { intc; c ==0? c = 2: c = 1; cout<< c << endl; } 2 #include<iostream> usingnamespace::std; voidmain() { intc; c ==2? c = 2: c = 1; cout<< c << endl; } 1
  • 104. Conditional Operator? #include<iostream> usingnamespace::std; voidmain() { intc; c!= 0? c = 2: c = 1; cout<< c << endl; } 1 #include<iostream> usingnamespace::std; voidmain() { intc; c ==0? 2: 1; cout<< c << endl; } 0
  • 105. Conditional Operator? #include<iostream> usingnamespace::std; voidmain() { intc; c!= 0? 2: 1; cout<< c << endl; } 0 #include<iostream> usingnamespace::std; voidmain() { intc; c!= 0? c=2: 1; cout<< c << endl; } 0
  • 106. Conditional Operator? #include<iostream> usingnamespace::std; voidmain() { intc; c = 0? c=2: 1; cout<< c << endl; } 1 (c = 0) Simply the variable c is assigned 0 If(zero) returns false
  • 109. Code Cracking #include<iostream> usingnamespace::std; voidmain() { enumCars {Nissan,BMW,Mercedes, Ferrari,Renault=Nissan--}; Cars MyCar= Renault; } Compiler error Nissan-- #include<iostream> usingnamespace::std; voidmain() { enumCars{Nissan,BMW,Mercedes,Ferrari,Renault++}MyCar=Nissan; } Compiler error Renault++, why?
  • 110. Code Cracking #include<iostream> usingnamespace::std; voidmain() { enumCars { Nissan = 2.4,BMW,Mercedes,Ferrari,Renault }MyCar= Nissan; MyCar= Renault; inti1 = MyCar; } Compiler error Nissan = 2.4 must be integer #include<iostream> usingnamespace::std; voidmain() { intc = 1, a; if((a = 0 ) && (c = 2)) { c = 9; } cout<< c << endl; } 9
  • 111. Code Cracking #include<iostream> usingnamespace::std; voidmain() { intc = 1, a = 0; if((a = 3) > 2 ) { c = 9; } cout<< c << endl; } 9 #include<iostream> usingnamespace::std; constx = 20; voidmain() { } 2008 Compiler: Compiler error no default intsupported 2005 Compiler: Compile & Run Intassumed
  • 112. Code Cracking #include<iostream> usingnamespace::std; voidmain() { intc; if(c++== 1) { cout<< "joo"<<endl; } else { cout<< "yoo"<< endl; } } yoo #include<iostream> usingnamespace::std; voidmain() { intc; if(++c == 1) { cout<< "joo"<<endl; } else { cout<< " "<< endl; } } joo