
Module 2 Introducing Data Types and Operators
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
Module 2 Introducing Data Types and Operators Module 2 Introducing Data Types and OperatorsTable of ContentsCRITICAL SKILL 2.1: The C++ Data Types ....................................................................................................... 2Project 2-1 Talking to Mars ......................................................................................................................... 10CRITICAL SKILL 2.2: Literals ......................................................................................................................... 12CRITICAL SKILL 2.3: A Closer Look at Variables ........................................................................................... 15CRITICAL SKILL 2.4: Arithmetic Operators................................................................................................... 17CRITICAL SKILL 2.5: Relational and Logical Operators ................................................................................ 20Project 2-2 Construct an XOR Logical Operation ........................................................................................ 22CRITICAL SKILL 2.6: The Assignment Operator ........................................................................................... 25CRITICAL SKILL 2.7: Compound Assignments .............................................................................................. 25CRITICAL SKILL 2.8: Type Conversion in Assignments ................................................................................. 26CRITICAL SKILL 2.9: Type Conversion in Expressions .................................................................................. 27CRITICAL SKILL 2.10: Casts........................................................................................................................... 27CRITICAL SKILL 2.11: Spacing and Parentheses ........................................................................................... 28Project 2-3 Compute the Regular Payments on a Loan .............................................................................. 29At the core of a programming language are its data types and operators. These elements define thelimits of a language and determine the kind of tasks to which it can be applied. As you might expect, C++supports a rich assortment of both data types and operators, making it suitable for a wide range ofprogramming. Data types and operators are a large subject. We will begin here with an examination ofC++’s foundational data types and its most commonly used operators. We will also take a closer look atvariables and examine the expression. 1 C++ A Beginner’s Guide by Herbert SchildtWhy Data Types Are ImportantThe data type of a variable is important because it determines the operations that are allowed and therange of values that can be stored. C++ defines several types of data, and each type has uniquecharacteristics. Because data types differ, all variables must be declared prior to their use, and a variabledeclaration always includes a type specifier. The compiler requires this information in order to generatecorrect code. In C++ there is no concept of a “type-less” variable.A second reason that data types are important to C++ programming is that several of the basic types areclosely tied to the building blocks upon which the computer operates: bytes and words. Thus, C++ letsyou operate on the same types of data as does the CPU itself. This is one of the ways that C++ enablesyou to write very efficient, system-level code.CRITICAL SKILL 2.1: The C++ Data TypesC++ provides built-in data types that correspond to integers, characters, floating-point values, andBoolean values. These are the ways that data is commonly stored and manipulated by a program. As youwill see later in this book, C++ allows you to construct more sophisticated types, such as classes,structures, and enumerations, but these too are ultimately composed of the built-in types.At the core of the C++ type system are the seven basic data types shown here:C++ allows certain of the basic types to have modifiers preceding them. A modifier alters the meaning ofthe base type so that it more precisely fits the needs of various situations. The data type modifiers arelisted here:signedunsignedlongshortThe modifiers signed, unsigned, long, and short can be applied to int. The modifiers signed and unsignedcan be applied to the char type. The type double can be modified by long. Table 2-1 shows all valid 2 C++ A Beginner’s Guide by Herbert Schildtcombinations of the basic types and the type modifiers. The table also shows the guaranteed minimumrange for each type as specified by the ANSI/ISO C++ standard.It is important to understand that minimum ranges shown in Table 2-1 are just that: minimum ranges. AC++ compiler is free to exceed one or more of these minimums, and most compilers do. Thus, the rangesof the C++ data types are implementation dependent. For example, on computers that use two’scomplement arithmetic (which is nearly all), an integer will have a range of at least −32,768 to 32,767. Inall cases, however, the range of a short int will be a subrange of an int, which will be a subrange of along int. The same applies to float, double, and long double. In this usage, the term subrange means arange narrower than or equal to. Thus, an int and long int can have the same range, but an int cannot belarger than a long int.Since C++ specifies only the minimum range a data type must support, you should check your compiler’sdocumentation for the actual ranges supported. For example, Table 2-2 shows typical bit widths andranges for the C++ d ...
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 giáo trình tin học giáo trình C++ Module 2Tài liệu có liên quan:
-
Giáo trình Tin học (Trình độ: Trung cấp nghề) - Trường Trung cấp nghề Củ Chi
268 trang 382 4 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 -
122 trang 222 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 -
Giáo Trình tin học căn bản - ĐH Marketing
166 trang 202 0 0 -
tài liệu môn Kinh tế vĩ mô_chương 1
10 trang 201 0 0 -
20 trang 190 0 0
-
BÁO CÁO KHẢO SÁT ĐỊA CHẤT CÔNG TRÌNH
33 trang 186 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 -
Hướng dẫn tạo file ghost và bung ghost
12 trang 161 0 0 -
Quyết định cấu trúc vốn trong thực tiễn
trang 155 0 0 -
5 trang 146 0 0
-
Giáo trình nhập môn lập trình - Phần 22
48 trang 143 0 0 -
MẪU ĐƠN ĐỀ NGHỊ CÔNG NHẬN VĂN BẰNG DO CƠ SỞ GIÁO DỤC NƯỚC NGOÀI CẤP
3 trang 116 0 0 -
Thủ thuật khôi phục mật khẩu Windows XP
3 trang 104 0 0 -
5 trang 102 1 0
-
Những nội dung cơ bản khi xây dựng hệ thống bài thực hành cho các môđun trong đào tạo nghề
5 trang 99 0 0 -
Bài giảng học với MẠNG MÁY TÍNH
107 trang 97 0 0 -
BÁO CÁO THỰC TẬP TỐT NGHIỆP: Kế toán Nguyên vật liệu và Công cụ dụng cụ
7 trang 96 0 0 -
Luận văn Nâng cao năng lực tự học cho HS chuyên Hoá học bằng tài liệu tự học có hướng dẫn theo modun
162 trang 86 0 0