SlideShare a Scribd company logo
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 (20)

PDF
C++ L10-Inheritance
Mohammad Shaker
 
PDF
C++ L09-Classes Part2
Mohammad Shaker
 
PDF
c programming
Arun Umrao
 
PPTX
Unit 3
GOWSIKRAJAP
 
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
PDF
c programming
Arun Umrao
 
PDF
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
ssuserd6b1fd
 
PDF
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
kinan keshkeh
 
PPTX
13 Strings and Text Processing
Intro C# Book
 
PDF
Programming with GUTs
Kevlin Henney
 
DOC
C tech questions
vijay00791
 
PPT
Fp201 unit5 1
rohassanie
 
PDF
Modern C++ Concurrency API
Seok-joon Yun
 
PPTX
C++ Pointers
Chaand Sheikh
 
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
ssuserd6b1fd
 
PDF
C++ Programming - 11th Study
Chris Ohk
 
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
ssuserd6b1fd
 
PDF
Functional C++
Kevlin Henney
 
PDF
What We Talk About When We Talk About Unit Testing
Kevlin Henney
 
PPTX
06.Loops
Intro C# Book
 
C++ L10-Inheritance
Mohammad Shaker
 
C++ L09-Classes Part2
Mohammad Shaker
 
c programming
Arun Umrao
 
Unit 3
GOWSIKRAJAP
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
c programming
Arun Umrao
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
ssuserd6b1fd
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
kinan keshkeh
 
13 Strings and Text Processing
Intro C# Book
 
Programming with GUTs
Kevlin Henney
 
C tech questions
vijay00791
 
Fp201 unit5 1
rohassanie
 
Modern C++ Concurrency API
Seok-joon Yun
 
C++ Pointers
Chaand Sheikh
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
ssuserd6b1fd
 
C++ Programming - 11th Study
Chris Ohk
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
ssuserd6b1fd
 
Functional C++
Kevlin Henney
 
What We Talk About When We Talk About Unit Testing
Kevlin Henney
 
06.Loops
Intro C# Book
 

Viewers also liked (18)

PPTX
Java script
Abhishek Kesharwani
 
KEY
Inside jQuery (2011)
Kenneth Auchenberg
 
PPT
JSON - Quick Overview
Signure Technologies
 
PPT
Java Script Object Notation (JSON)
Adnan Sohail
 
PDF
Fundamental JQuery
Achmad Solichin
 
PDF
Introduction to JSON
Kanda Runapongsa Saikaew
 
PDF
jQuery for beginners
Siva Arunachalam
 
PPTX
HTML/CSS/java Script/Jquery
FAKHRUN NISHA
 
PPT
A quick guide to Css and java script
AVINASH KUMAR
 
PPT
Java script
Soham Sengupta
 
PPTX
jQuery from the very beginning
Anis Ahmad
 
PPTX
Java script
Jay Patel
 
PPT
Java script final presentation
Adhoura Academy
 
PPTX
Java script
Sadeek Mohammed
 
PPTX
Java script
Shyam Khant
 
PPTX
An Overview of HTML, CSS & Java Script
Fahim Abdullah
 
PPTX
JSON: The Basics
Jeff Fox
 
PDF
jQuery Essentials
Marc Grabanski
 
Java script
Abhishek Kesharwani
 
Inside jQuery (2011)
Kenneth Auchenberg
 
JSON - Quick Overview
Signure Technologies
 
Java Script Object Notation (JSON)
Adnan Sohail
 
Fundamental JQuery
Achmad Solichin
 
Introduction to JSON
Kanda Runapongsa Saikaew
 
jQuery for beginners
Siva Arunachalam
 
HTML/CSS/java Script/Jquery
FAKHRUN NISHA
 
A quick guide to Css and java script
AVINASH KUMAR
 
Java script
Soham Sengupta
 
jQuery from the very beginning
Anis Ahmad
 
Java script
Jay Patel
 
Java script final presentation
Adhoura Academy
 
Java script
Sadeek Mohammed
 
Java script
Shyam Khant
 
An Overview of HTML, CSS & Java Script
Fahim Abdullah
 
JSON: The Basics
Jeff Fox
 
jQuery Essentials
Marc Grabanski
 
Ad

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

PDF
2.Types_Variables_Functions.pdf
TrnThBnhDng
 
PPT
Key Concepts of C++ computer language.ppt
AjayLobo1
 
PPTX
Data Type Conversion in C++
Danial Mirza
 
PDF
BASIC C++ PROGRAMMING
gufranresearcher
 
PDF
Data types in c++
RushikeshGaikwad28
 
PDF
OOP UNIT 1_removed ppt explaining object.pdf
rajbaibhav004
 
PDF
Programming in C++
Pooya Merat
 
PDF
Object Oriented Programming Short Notes for Preperation of Exams
MuhammadTalha436
 
PPTX
C++ 11 Features
Jan Rüegg
 
PDF
2 BytesC++ course_2014_c1_basicsc++
kinan keshkeh
 
PPT
chap1cpp3rd.ppt
krishnakumararunacha5
 
PPTX
Cs1123 4 variables_constants
TAlha MAlik
 
PPTX
Object Oriented Programming with C++
Rokonuzzaman Rony
 
PPT
Data Handling
Praveen M Jigajinni
 
TXT
Advance C++notes
Rajiv Gupta
 
PPTX
Operators
moniammu
 
PPTX
C++ theory
Shyam Khant
 
PDF
Computer science-2010-cbse-question-paper
Deepak Singh
 
PPT
Chapter02-S11.ppt
GhulamHussain638563
 
2.Types_Variables_Functions.pdf
TrnThBnhDng
 
Key Concepts of C++ computer language.ppt
AjayLobo1
 
Data Type Conversion in C++
Danial Mirza
 
BASIC C++ PROGRAMMING
gufranresearcher
 
Data types in c++
RushikeshGaikwad28
 
OOP UNIT 1_removed ppt explaining object.pdf
rajbaibhav004
 
Programming in C++
Pooya Merat
 
Object Oriented Programming Short Notes for Preperation of Exams
MuhammadTalha436
 
C++ 11 Features
Jan Rüegg
 
2 BytesC++ course_2014_c1_basicsc++
kinan keshkeh
 
chap1cpp3rd.ppt
krishnakumararunacha5
 
Cs1123 4 variables_constants
TAlha MAlik
 
Object Oriented Programming with C++
Rokonuzzaman Rony
 
Data Handling
Praveen M Jigajinni
 
Advance C++notes
Rajiv Gupta
 
Operators
moniammu
 
C++ theory
Shyam Khant
 
Computer science-2010-cbse-question-paper
Deepak Singh
 
Chapter02-S11.ppt
GhulamHussain638563
 
Ad

More from Mohammad Shaker (20)

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

Recently uploaded (20)

PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPT
Electrical Safety Presentation for Basics Learning
AliJaved79382
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Thermal runway and thermal stability.pptx
godow93766
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Electrical Safety Presentation for Basics Learning
AliJaved79382
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 

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