Electrical Engineering - Chapter 3: Variables and Arithmetic Operations

Words with special meaning to C++ Also includes alternative representations of certain operators and punctuators Full listing in Table 3.1 Examples: auto, bool, float, inline, union, delete namespace, private, void

ppt25 trang | Chia sẻ: thuongdt324 | Lượt xem: 412 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Electrical Engineering - Chapter 3: Variables and Arithmetic Operations, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Chapter 3 – Variables and Arithmetic OperationsVariable RulesMust declare all variable namesList name and typeKeep length to 31 charactersOlder compiler restrictionGive numeric values to variables with assignment statementLesson 3.1variable_name = value;assignment operatorNaming IdentifiersFirst character must be lettera-z, A-Z or _Other charactersletters (a-z, A-Z, _ ) or digits 0-9Cannot use C++ keywords (reserved words)Cannot have blank within identifiesLesson 3.1KeywordsWords with special meaning to C++Also includes alternative representations of certain operators and punctuatorsFull listing in Table 3.1Examples:auto, bool, float, inline, union, deletenamespace, private, voidLesson 3.1Declaring VariablesVariables MUST be declaredList name and data typeVariables of same type may be declared in same statement (separate by comma)Causes C++ compiler to know size of space to be reserved for storing variable’s valueLesson 3.1double radius, diameter;data typevariable namesseparatorAssignment StatementsCauses value to be stored in variable’s memory cellvariable_name = value;Variable name MUST appear on leftEqual sign is assignment operatorNote: be careful = does NOT mean equalExample:temperature = 78;Lesson 3.1Constant Qualified VariablesUse const qualifier const double PI = 3.14159;Cannot modify later in programStyle tip: use all uppercase characters to name constantMakes constants easy to identifyLesson 3.2Formatting Output Insert I/O manipulators (parameterized) into cout statements for printingdeclared in header iomanip #include Basic form cout (expression)Keyword requires underscoreExpression for which temporary copy is made of type typeType can be any valid C++ data typeOperator PrecedenceLesson 3.5( ) parentheses unary prefix L to R 1++, -- post-(in/de)crement unary postfix L to R 2++, -- pre-(in/de)crement unary prefix R to L 3 + positive sign unary prefix R to L 3 - negative sign unary prefix R to L 3static_cast cast unary prefix R to L 4%, *, / remainder/multi/div binary infix L to R 5+, - add/subtract binary infix L to R 6+=, -=, *= math & assignment binary infix R to L 7 /=, %= math & assignment binary infix R to L 7 = assignment binary infix R to L 7Real data typesDecimal numbersfloat4 bytes of memory, 6 digit precisiondouble8 bytes of memory, 15 digits precisionlong double10 bytes of memory, 19 digits precisionLesson 3.6Integer data typesWhole numbersint, signed int, short int, signed short int2 bytes, range: -32768 to 32767unsigned int, unsigned short int2 bytes, range: 0 to 65535long int, signed long int4 bytes, range: -2147483648 to 2147483645unsigned long int4 bytes, range: 0 to 4294967295Lesson 3.6Math FunctionsNeed cmath or cstlib headers #include or #include General form: name(parameters)Note what type and form parameters takeTrig functions use radian measure not angleTable 3.11 lists math library functionsLesson 3.6SummaryDeclare variables and constantsFormat outputWork with character dataCreate mathematical expressionsWork with mixed data types and castingUse different data types for precisionUtilize available math functionsChapter 3Learned how to: