
Kỹ thuật lập trình_Module 7
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
Kỹ thuật lập trình_Module 7 Module 7 More Data Types and OperatorsTable of ContentsCRITICAL SKILL 7.1: The const and volatile Qualifiers ................................................................................... 2CRITICAL SKILL 7.2: extern............................................................................................................................. 5CRITICAL SKILL 7.3: static Variables .............................................................................................................. 6CRITICAL SKILL 7.4: register Variables ......................................................................................................... 10CRITICAL SKILL 7.5: Enumerations .............................................................................................................. 12CRITICAL SKILL 7.6 typedef.......................................................................................................................... 16CRITICAL SKILL 7.8: The Shift Operators ..................................................................................................... 22CRITICAL SKILL 7.9: The ? Operator ............................................................................................................ 29CRITICAL SKILL 7.10: The Comma Operator ................................................................................................ 31CRITICAL SKILL 7.11: Compound Assignment ............................................................................................. 33CRITICAL SKILL 7.12: Using sizeof ................................................................................................................ 33This module returns to the topics of data types and operators. In addition to the data types that youhave been using so far, C++ supports several others. Some of these consist of modifiers added to thetypes you already know about. Other data types include enumerations and typedefs. C++ also providesseveral additional operators that greatly expand the range of programming tasks to which C++ can beapplied. These operators include the bitwise, shift, ?, and sizeof operators. 1 C++ A Beginner’s Guide by Herbert SchildtCRITICAL SKILL 7.1: The const and volatile QualifiersC++ has two type qualifiers that affect the ways in which variables can be accessed or modified. Thesemodifiers are const and volatile. Formally called the cv-qualifiers, they precede the base type when avariable is declared.constA variable declared with the const modifier cannot have its value changed during the execution of yourprogram. Thus, a const “variable” isn’t really variable! You can give a variable declared as const an initialvalue, however. For example,const int max_users = 9;creates an int variable called max_users that contains the value 9. This variable can be used inexpressions like any other variable, but its value cannot be modified by your program.A common use of const is to create a named constant. Often programs require the same value for manydifferent purposes. For example, a program might have several different arrays that are all the samesize. In this case, you can specify the size of the arrays using a const variable. The advantage to thisapproach is that if the size needs to be changed at a later date, you need change only the value of theconst variable and then recompile the program. You don’t need to change the size in each arraydeclaration. This approach avoids errors and is easier, too. The following example illustrates thisapplication of const:In this example, if you need to use a new size for the arrays, you need change only the declaration ofnum_employees and then recompile the program. All three arrays will then automatically be resized.Another important use of const is to prevent an object from being modified through a pointer. Forexample, you might want to prevent a function from changing the value of the object pointed to by aparameter. To do this, declare a pointer parameter as const. This prevents the object pointed to by theparameter from being modified by a function. That is, when a pointer parameter is preceded by const, 2 C++ A Beginner’s Guide by Herbert Schildtno statement in the function can modify the variable pointed to by that parameter. For example, thenegate( ) function in the following program returns the negation of the value pointed to by itsparameter. The use of const in the parameter declaration prevents the code inside the function frommodifying the value pointed to by the parameter.Since val is declared as being a const pointer, the function can make no changes to the value pointed toby val. Since negate( ) does not attempt to change val, the program compiles and runs correctly.However, if negate( ) were written as shown in the next example, a compile-time error would result.In this case, the program attempts to alter the value of the variable pointed to by val, which is preventedbecause val is declared as const.The const modifier can also be used on reference parameters to prevent a function from modifying theobject referenced by a parameter. For example, the following version of negate( ) is incorrect because itattempts to modify the variable referred to by val: 3 C++ A Beginner’s Guide by Herbert SchildtvolatileThe volatile modifier tells the compiler that a variable’s value may be changed in ways not explicitlyspecified by the program. For example, the address of a global variable might be passed to aninterrupt-driven clock routine that updates the variable with each tick of the clock. In this situation, thecontents of the ...
Tìm kiếm theo từ khóa liên quan:
Giáo dục đào tạo giáo trình cao đẳng đại học kỹ thuật lập trình ngôn ngữ lập trình lập trình C++Tài liệu có liên quan:
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 313 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 306 0 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 292 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 246 0 0 -
Bài giảng Một số hướng nghiên cứu và ứng dụng - Lê Thanh Hương
13 trang 245 0 0 -
Giáo trình Lập trình cơ bản với C++: Phần 1
77 trang 241 0 0 -
MẪU ĐƠN ĐỀ NGHỊ CẤP GIẤY PHÉP dạy thêm học thêm ngoài nhà trường
3 trang 240 2 0 -
Giáo án Tin học lớp 11 (Trọn bộ cả năm)
125 trang 230 1 0 -
Giới thiệu môn học Ngôn ngữ lập trình C++
5 trang 222 0 0 -
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 204 0 0 -
MẪU ĐƠN XIN XÉT TUYỂN VÀO LỚP 10 TRƯỜNG THPT DÂN TỘC NỘI TRÚ TỈNH
2 trang 202 0 0 -
tài liệu môn Kinh tế vĩ mô_chương 1
10 trang 201 0 0 -
Thiết kế mạch logic bằng Verilog - HDL
45 trang 193 0 0 -
20 trang 190 0 0
-
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 188 0 0 -
BÁO CÁO KHẢO SÁT ĐỊA CHẤT CÔNG TRÌNH
33 trang 187 0 0 -
Giáo trình Lập trình C căn bản: Phần 1
64 trang 174 0 0 -
Báo cáo thực tập tốt nghiệp môn Điện - Điện tử: Thiết lập hệ thống mạng
25 trang 167 0 0 -
Báo cáo thực tập: Quản lý nhân sự & tiền lương
52 trang 160 0 0 -
Luận văn: Nghiên cứu kỹ thuật giấu tin trong ảnh Gif
33 trang 159 0 0