Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd 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, 3rd 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, 3rd 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 (expression) int a = static_cast 3.5*23; Program 3.1
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd 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, 3rd Ed. APress Media, LLC. Numeric Limitation numeric_limits ::min() numeric_limits ::max() numeric_limits ::digits Program 3.3 Header file –limits.h
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Boolean Operators T: true; F: false PQP && QP || Q!P TTTTF TFFTF FTFTT FFFFT
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Bitwise operators
Bitwise operations bit1bit2&|^~bit : true, 0: false
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd 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 << –( )<<3 ( ) Right shift >> –( )>>2 ( ) Bitwise complement ~ –~( ) ( )
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Bitwise exclusive OR operations Program 3.4
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Bitwise exclusive OR operations Program 3.4
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Number System 0x1AF = 1* * = 431 0xCAD = 12* * = 3245 o123 = 1*8 2 +2* = 83 o10101 = 1*8 4 +0*8 3 +1*8 2 +0* = x1AF = ( ) = o717 o123 = ( ) = 0x53
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd 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, 3rd 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, 3rd 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, 3rd 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, 3rd Ed. APress Media, LLC. Anonymous Enumerations –enum {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} yesterday, today, tommorrow; –enum {feetPerYard = 3, inchesPerFoot = 12, yardsPerMile = 1760}; –cout << “Feet in 5 miles= “ << 5*feetPerYard*yeardPerMile;
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd 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 (day_value); –enum Height {Bottom, Top = 20} position; –position = static_cast (10); Program 3.5
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd 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, 3rd 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, 3rd Ed. APress Media, LLC. Association for the operators OperatorAssociationOperatorAssociation Postfix ++LeftBinary + -Left Postfix --Left >Left Unary +Right& | ^ ~Left Unary -Right* / %Left Prefix ++Right= Prefix --Rightstatic_cast()Right
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Operator Precedence Appendix D
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Alterative types typedef long BigOnes; –BigOnes mynum = 0L; –long mynum = 0L;// same as previous typedef int EventCount; typedef long EventCount; // redefine EventCoutn the larger range of long integer