Basic Elements of C++ (I)

Basic Elements of C++ (I) 1. Token The smallest individual unit of a program written in any language is called a token. C++ tokens are divided into sp...
Author: Blaise Cox
54 downloads 0 Views 176KB Size
Basic Elements of C++ (I) 1. Token The smallest individual unit of a program written in any language is called a token. C++ tokens are divided into special symbols, word symbols, and identifiers.

1) Special Symbols Special symbols include mathematical symbols and punctuation marks. Example:

2) Word Symbols Word symbols, also called reserved words or keywords, are another type of token. Reserved words are always lowercase and each is considered to be a single symbol and have a special meaning. Example: Int Float Double Char Void Return

3) Identifiers Identifiers are used as names to variables, constants, and functions. Identifiers can be uppercase letters, lowercase letters, and the underline symbol in any order. Identifiers can also contain digits but cannot begin with a digit. C++ is case sensitive. So, Age, AGE, aGE and AgE could be names for different variables, although this is not recommended since it would probably cause confusion and errors in your programs. Some predefined identifiers are cout and cin. Unlike reserved words, predefined identifiers may be redefined, but it is not a good idea. !

2. Data Types Data Type: set of values together with a set of operations is called a data type C++ data can be classified into three categories: Simple data type, Structured data type, Pointers

Simple Data Types The categories of simple data types are integral, floating-point, and enumerated.

1) Integral Integral types are further classified as: char, short, int, long, bool, unsigned char, unsigned short, unsigned int, and unsigned long. Each data type has a different set of values associated with it. The type determines how big a number can be represented. The data type determines the amount of memory used. Different compilers may have a different range of values for each data type. a) int Data Type The int data type has numbers with no decimal point. Rules for the int data type are 1. Positive integers do not need + sign in front of them. 2. No commas are used within an integer. Commas are used for separating items in a list 3. Examples: -6728 0 78 4 b) bool Data Type The bool data type has two values: true and false called logical (Boolean) values. An expression that evaluates to true or false is called a logical (Boolean) expression. In C++, bool, true, and false are reserved words. c) char Data Type The char data type can hold the numeric values –128 to 127. The smallest integral data type It is used to represent characters—letters, digits, and special characters. There is a char data type to represent each key on the keyboard. The char data values are represented within single quotation marks. The blank is represented as ‘ ‘ with a space left between the single quotes Some of the values belonging to char data type are: 'A', 'a', '0', '*', '+', '$', '&'

2) Floating-Point Data Types Floating-point data types deal with numbers with decimal points. To represent real numbers, C++ uses a form of scientific notation called floating-point notation. In the C++ floating-point notation, the letter E stands for the exponent. Floating-point data types are further classified as float, double, and long double. Like integral types, each type determines the amount of memory used. a) Float: represents any real number Range: -3.4E+38 to 3.4E+38 Memory allocated for the float type is 4 bytes Maximum number of significant digits (decimal places) for float values is 6 or 7 Float values are called single precision Precision: maximum number of significant digits b) Double: represents any real number Range: -1.7E+308 to 1.7E+308 Memory allocated for double type is 8 bytes Maximum number of significant digits for double is 15 Double values are called double precision

String Type The data type string is a programmer-defined data type. To use this data type, the string library must be included. A string is a sequence of zero or more characters. Strings are enclosed in double quotation marks. Every character in a string has a relative position in the string. The position of the first character is 0. Then length of a string is the number of characters in it. Null: a string with no characters Must have a preprocessor command: #include

3. Arithmetic Operators and Operator Precedence 1) Arithmetic operators Arithmetic operators used for calculation of both integral and floating-point data types are +, -, *, /, and %. The modulus operator (%) is only used with integral types; +, -, *, and / can use with integral and floating-point data types. When the divide operation occurs with integral values, the quotient is truncated, not rounded. Expressions are written using these arithmetic operators. Operators that have two operands are called binary operators. An operator that has only one operand is a unary operator.

2) Order of Precedence The order of precedence is the same when programming as it is in mathematics a) All operations inside of ( ) are evaluated first b) *, /, and % are at the same level of precedence and are evaluated next c) + and have the same level of precedence and are evaluated last d) When operators are on the same level, performed from left to right

4. Expressions An arithmetic expression is constructed by using arithmetic operators and numbers.

1) integral expression If all operands in an expression are integers, the expression is called an integral expression. An integral expression yields integral result.

2) floating-point or decimal expression If all operands in an expression are floating-point numbers, the expression is called a floating-point or decimal expression. A floating-point expression yields a floating-point result.

3) Mixed Expressions An expression that has operands of different data types is called a mixed expression. Examples of mixed expressions: − 2 + 3.5 − 6 / 4 + 3.9 − 5.4 * 2 13.6 + 18 / 2 When evaluating mixed expressions: − if the operator has the same types of operands the operator is evaluated according to the type of the operands.

− If the operator has both types of operands, then during calculation the integer is changed to a floating-point number with the decimal part of zero and the operator is evaluated. The result is a floating-point number. − Following the rules of precedence, one operator at a time is considered.

5. Type Conversion (Casting) 1) Implicit type coercion: When a value of one data type is automatically changed to another data type, implicit type coercion is said to have occurred.

2) Explicit type conversion: To avoid implicit type coercion, C++ provides for explicit type conversion through the use of a cast operator, also called type conversion or typecasting. Type casting occurs in two forms: 1. The more stable form is using the reserved word static_cast. The format is static_cast (expression). 2. The other form is dataType (expression).