Download presentation
Presentation is loading. Please wait.
Published byAmice Walker Modified over 8 years ago
1
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
2
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
3
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
4
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
5
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
6
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
7
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Bitwise operators
8
Bitwise operations bit1bit2&|^~bit1 000001 010111 100110 111100 1: true, 0: false
9
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 << –(0011 1101)<<3 (1110 1000) Right shift >> –(0011 1101)>>2 (0000 1111) Bitwise complement ~ –~(0011 1101) (1100 0010)
10
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Bitwise exclusive OR operations Program 3.4
11
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Bitwise exclusive OR operations Program 3.4
12
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Number System 0x1AF = 1*16 2 +10*16 1 +15 = 431 0xCAD = 12*16 2 +10*16 1 +13 = 3245 o123 = 1*8 2 +2*8 1 +3 = 83 o10101 = 1*8 4 +0*8 3 +1*8 2 +0*8 1 +1 = 4161 0x1AF = (0001 1100 1111) = o717 o123 = (001 010 011) = 0x53
13
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
14
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC.
15
Output Manipulators Header file –iomanip.h Behind cout << –setprecision, setw, setbase, setfill
16
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};
17
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
18
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;
19
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
20
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
21
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
22
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
23
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Operator Precedence Appendix D
24
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.