Danh mục tài liệu

Kỹ thuật lập trình_Module 7

Số trang: 36      Loại file: pdf      Dung lượng: 776.00 KB      Lượt xem: 15      Lượt tải: 0    
Xem trước 4 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu kỹ thuật lập trình_module 7, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
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ài liệu được xem nhiều:

Tài liệu có liên quan: