1 / 29

Chapter 2 Beginning to Program in C++

Chapter 2 Beginning to Program in C++. 2.1 Constants( 常量 ). Data in C++ : Constants ; variables Constants : does not change its value in a program. 整型: int. 浮点型: float. 字符型: char. 字符串型: String. 2.2 Variables( 变量 ). Variables : can vary its values in a program

cristobala
Download Presentation

Chapter 2 Beginning to Program in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2 Beginning to Program in C++

  2. 2.1 Constants(常量) • Data in C++:Constants;variables • Constants:does not change its value in a program. 整型:int 浮点型:float 字符型:char 字符串型:String

  3. 2.2 Variables(变量) • Variables:can vary its values in a program • a variable must be defined before it can be used. A variable is defined by giving it a data type and a name main( ) marks the point where a C++ program starts to execute and must appear once defines variables’ name and type Assign value to variables • The program statements are contained within the braces { and } • Each statement ends with ’ ;’

  4. 2.2 Variables • A variable can be given any name, called an identifierin C++, provided it is within the following rules • An identifier can only be constructed using letters, numerals or underscores (_). • An identifier must start with a letter or an underscore. • An identifier cannot be a C++ keyword. A keyword is a word that has a special meaning. • An identifier can contain any number of characters, but only the first thirty-one characters are significant to the C++ compiler. • A variable is like a box in the memory of the computer.

  5. 2.3 Simple output to the screen • #include <iostream.h> • directive causes the file to make available to the program; • <iostream.h> contains some C++ statements to make it easy to perform input and output • cout • a string of characters: • letters or numbers enclosed in “ ” • The variable name is not in quotation marks • cout: sends the data to cout • the stream insertion operator << is used to insert a string of characters into the output stream object cout • endl: go the start of the next line on the screen

  6. 2.4 Comments(注释) • Comments are added to a C++ program to make it more readable for the programmer • completely ignored by the compiler. • comments start with the characters // and end at the end of the line.

  7. 2.4 Comments • C++ comments cannot span more than one line. • Typically, comments are placed at the start of the program to describe the purpose of the program, the author, date written and any other relevant information • C-style comments begin with the characters /* and end with the characters */ is also used in C++ • Get into the habit of using comments • bad comments can lead to confusion

  8. 2.5 Data types(数据类型) • Different data types require different amounts of memory and therefore vary in the range of values they can store. • short integer:短整型 • Long Integer data types: 长整型 • Boolean data types:布尔数据类型 • can store only one of two values: true(1) or false(0) • Double Floating-point data types: increase the range and precision(or accuracy) of a floating-point number.

  9. 2.6 Data type sizes • sizeofoperator to display the number of bytes of memory required by some common data types. running results

  10. 2.7 Operators(运算符) • 2.7.1 The assignment operator(赋值运算符)“=” • assign values to variables. • 2.7.2 Arithmetic operators(代数运算符) %: 取余运算符,获取除法运算的余数

  11. 2.7 Operators • Program Example P2E running results

  12. 2.7 Operators • 2.7.3 Increment(自加)、 decrement(自减) operators • ++: adds 1 to the value of a variable • --:subtracts 1 from the variable var1 • The increment operator ++ has two forms, prefix and postfix

  13. 2.7 Operators • Program Example P2F • Line 9 of this program defines the variables var1 and var2 as integers and initialises them to 1 and 2, respectively. • Line 15 adds 1 to var1, and line 16 subtracts 1 from var2. • Lines 19 and 20 display the final value of var1 and var2.

  14. 2.7 Operators • Program Example P2G • prefix----If the ++ is before a variable, the variable is incremented before it is used. • postfix ----If the ++ is after the variable, the variable is used and then incremented. • The decrement operator -- also has prefix and postfix forms. 运行结果

  15. 2.7 Operators • 2.7.4 Combined assignment operators • The += operator adds the value on its right to the variable on its left. • There are five combined assignment operators: +=, -=, *=, /=, and %=, corresponding to the five arithmetic operators +, -, *, /, and %.

  16. 2.8 Operator precedence(运算符的优先级) • Consider the following statement: • 2 + 7 * 8 = 72 var • 2 + 7 * 8 = 58 var • Operator precedence × √ 1)*,/ have a higher priority than + 、- 2)Expressions containing operators of the same precedence are evaluated according to their associativity 3)use ()to change the order of evaluation

  17. 2.8 Operator precedence • Using parentheses will remove any confusion about which operations are done first. • unary minus: • “-” appears before an operand • highest priority • binary minus:appears between two operands

  18. 2.9 Type conversions and casts(类型转换和强转) • Automatic conversion-----Program Example P2H For calculations involving mixed data types, C++ automatically converts the value. running results

  19. 2.9 Type conversions and casts • When doing calculations involving mixed data types, C++ ranks the data types in this order: • promotion or wideningof the data • For calculations involving mixed data types, C++ automatically converts the value in the lower data type to a higher type. • Promotion will cause no loss of data, because the higher data types occupy more memory than the lower types and can therefore hold the data precisely.

  20. 2.9 Type conversions and casts • demotion or narrowing of the data • when data is assigned to a variable of a lower type • Demotion may result in a loss of data, because the lower data type may not have enough bytes of memory to store the higher data type.

  21. 2.9 Type conversions and casts • Manual conversion with a static cast • Using a static cast is equivalent to assigning the expression to a variable of the specified type and using that variable in place of the expression. The expression var1/4.0 is mixed, and the value of var1 is therefore promoted to a floating point value automatically.

  22. Programming pitfalls • 1. Do not type a semicolon after either • 2. End each C++ statement with a semicolon. • 3. A semicolon is not always at the end of a line. This will cause a compiler error, because the semicolon is part of the comment.

  23. Programming pitfalls • 4. A typing error may result in a statement that does nothing but that is valid nonetheless.

  24. Programming pitfalls • 5. You must initialise a variable before using it in an arithmetic expression. • 6. Be aware of the operator precedence rules. • If you are unsure, use parentheses. • The number of opening and closing parentheses should be equal. • 7. Each variable has an associated data type (int, float, etc.). Be careful not to go outside the range of valid values for a variable.

  25. Programming pitfalls • 8. You may not always get what you expect when doing arithmetic in C++. • variable f will contain 12, and not the 12.5 • Use a static cast if the fractional part of the result is required.

  26. Programming pitfalls • 9. Avoid unnecessary comments in your program.

  27. Quick syntax reference

  28. Quick syntax reference

  29. Q & A Thank You!

More Related