Refer to Ivor Hortons Beginning ANSI C The

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. More on Handling Basic Data Types • • • Mixed types of data Data type conversion Bitwise operators Define a new type Define alternative names for existing data types • Variables duration and scope

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Mixed expressions • Mixed expression rule – Two floating operation higher precision – Floating and Integer operation floating • See details in p. 92

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Explicit Casts • Static cast – Old-style casts (C) • (object_type) (expression) • int a = (int) 3. 5; or int a = 3. 5; – New-style casts (C++) • static_cast <object_type> (expression) • int a = static_cast <int> 3. 5*23; • Program 3. 1

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. sizeof() • Count the size of a type or a variable • Program 3. 2

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Numeric Limitation • • • numeric_limits <type_name>: : min() numeric_limits <type_name>: : max() numeric_limits <type_name>: : digits Program 3. 3 Header file – limits. h

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Boolean Operators P Q P && Q T T F T F F T T: true; F: false P || Q !P

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Bitwise operators

Bitwise operations 1: true, 0: false bit 1 bit 2 & | ^ ~bit 1 0 0 0 1 0 1 1 0 0 1 1 1 1 0 0

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Bitwise Operators • Bitwise AND & – 0&0 0 0&1 0 1&1 1 • Bitwise OR | – 0|0 0 0|1 1 1|1 1 • Bitwise XOR ^ – 0^0 0 0^1 1 1^1 0 • Left shift << – (0011 1101)<<3 (1110 1000) • Right shift >> – (0011 1101)>>2 (0000 1111) • Bitwise complement ~ – ~(0011 1101) (1100 0010)

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Bitwise exclusive OR operations • Program 3. 4

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Bitwise exclusive OR operations • Program 3. 4

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Number System • • 0 x 1 AF = 1*162+10*161+15 = 431 0 x. CAD = 12*162+10*161+13 = 3245 o 123 = 1*82+2*81+3 = 83 o 10101 = 1*84+0*83+1*82+0*81+1 = 4161 • 0 x 1 AF = (0001 1100 1111) = o 717 • o 123 = (001 010 011) = 0 x 53

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Output Manipulators • Header file – iostream. h • Behind cout << – dec, hex, oct, left, right, fixed, scientific, showpoint, noshowpoint, showbase, noshowbase, showpos, noshowpos, uppercase, nouppercase, boolalpha, noboolalpha

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC.

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Output Manipulators • Header file – iomanip. h • Behind cout << – setprecision, setw, setbase, setfill

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Enumerated Data Types • Limited range with name – enum Weekday { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; – Weekday today = Tuesday; // 1 – enum Weekday { Monday=1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; – enum Weekday {Monday=1, Mon=1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Un-unique Data Enumerations – enum Weekday { Monday, Mon = Monday, Tuesday = Monday+2, Tues = Tuesday, Wednesday = Tuesday+2, Wed = Wednesday, Thursday = Wednesday+2, Thurs = Thursday, Friday = Thursday +2, Fri = Friday, Saturday = Friday +2, Sat = Saturday, Sunday = Saturday+2, Sun = Sunday}; // 0~12 – enum Punctuation {Comma = ‘, ’ , Exclamation=‘!’, Question = ‘? ’}; // 33~63

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Anonymous Enumerations – enum {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} yesterday, tommorrow; – enum {feet. Per. Yard = 3, inches. Per. Foot = 12, yards. Per. Mile = 1760}; – cout << “Feet in 5 miles= “ << 5*feet. Per. Yard*yeard. Per. Mile;

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Casting between Integer and Enumeration Types • The enumerators are constants – Weekday today = Tuesday; – int day_value = today+1; – today = day_value; //error – today = static_cast<Weekday>(day_value); – enum Height {Bottom, Top = 20} position; – position = static_cast<Height>(10); • Program 3. 5

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Lifetime of a Variable • A variable can have three different kinds of storage duration – Automatic storage duration – Static storage duration – Dynamic storage duration

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Variable Scopes • Automatic variables or local variables – Variables declared in block – Program 3. 6 • Global variables – Variables declared outside of blocks – : : scope resolution operator – Program 3. 7 • Static variables (local access, global exist) • External variables

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Association for the operators Operator Postfix ++ Postfix -Unary + Unary Prefix ++ Prefix -- Association Operator Association Left Binary + Left << >> Left Right & | ^ ~ Left Right * / % Left Right = Right static_cast() Right

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Operator Precedence • Appendix D

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3 rd Ed. APress Media, LLC. Alterative types • typedef long Big. Ones; – Big. Ones mynum = 0 L; – long mynum = 0 L; // same as previous • typedef int Event. Count; • typedef long Event. Count; // redefine Event. Coutn the larger range of long integer
- Slides: 24