Содержание
- 2. OBJECTIVES In this lecture you will learn: To write simple computer programs in C++. To write
- 3. Introduction C++ Programming Facilitates disciplined approach to computer program design Programs process information and display results
- 4. First Program in C++: Printing a Line of Text Simple Program Prints a line of text
- 5. Outline fig02_01.cpp (1 of 1) fig02_01.cpp output (1 of 1)
- 6. Good Programming Practice Every program should begin with a comment that describes the purpose of the
- 7. Common Programming Error Forgetting to include the header file in a program that inputs data from
- 8. Common Programming Error Syntax errors are also called compiler errors, compile-time errors or compilation errors, because
- 11. Tokens: The smallest individual units of a program are called tokens. Constants Variables Keywords Data Types
- 12. Constants , Identifiers and Keywords The alphabets , numbers and special symbols when properly combined form
- 13. Following are the rules for naming identifiers: Only alphabetic characters digits and underscores are permitted. The
- 14. Keywords Keywords implement specific C++ language features. They are explicitly reserved identifiers and cannot be used
- 16. Data Type Data Type : type of data to be stored in a variable Primitive data
- 17. Primitive Data Type Primitive Type I N T E G E R R E A L
- 18. Primitive Data Type Why do we need to define the type of data? Efficient use of
- 19. Primitive Data Type sizeof operator Return memory size of operand in byte Need () when the
- 20. Primitive Data Type Criteria of selection of data type Real type data Accuracy ‘double’ is common
- 21. Primitive Data Type Example #include #include using namespace std; int main(void) { double radius; double area;
- 22. Primitive Data Type unsigned: the range of data is changed Positive integer only Can not be
- 23. Primitive Data Type How to express letter (including characters, notations, …) inside computer? ASCII (American Standard
- 24. Primitive Data Type Range of ASCII code 0 ~ 127, ? possible using ‘char’ type Declare
- 25. Primitive Data Type Example #include using namespace std; int main(void) { char ch1='A'; char ch2=65; cout
- 26. Primitive Data Type ASCII code
- 27. Symbolic Constant Make ‘variable’ to ‘constant’ #include using namespace std; int main(void) { const int MAX
- 28. Another C++ Program: Adding Integers Variables Location in memory where value can be stored Common data
- 29. Another C++ Program: Adding Integers (Cont.) Variables (Cont.) Can declare several variables of same type in
- 30. Outline fig02_05.cpp (1 of 1) fig02_05.cpp output (1 of 1) Declare integer variables Use stream extraction
- 31. Good Programming Practice Place a space after each comma (,) to make programs more readable.
- 32. Good Programming Practice Some programmers prefer to declare each variable on a separate line. This format
- 33. Portability Tip C++ allows identifiers of any length, but your C++ implementation may impose some restrictions
- 34. Good Programming Practice Choosing meaningful identifiers helps make a program self-documenting—a person can understand the program
- 35. Good Programming Practice Always place a blank line between a declaration and adjacent executable statements. This
- 36. Another C++ Program: Adding Integers (Cont.) Input stream object std::cin from Usually connected to keyboard Stream
- 37. Another C++ Program: Adding Integers (Cont.) Assignment operator = Assigns value on left to variable on
- 38. Another C++ Program: Adding Integers (Cont.) Concatenating stream insertion operations Use multiple stream insertion operators in
- 39. Memory Concept Variable names Correspond to actual locations in computer's memory Every variable has name, type,
- 40. Fig. 2.6 | Memory location showing the name and value of variable number1.
- 41. Fig. 2.7 | Memory locations after storing values for number1 and number2.
- 42. Fig. 2.8 | Memory locations after calculating and storing the sum of number1 and number2.
- 43. Arithmetic Arithmetic operators * Multiplication / Division Integer division truncates remainder 7 / 5 evaluates to
- 44. Common Programming Error Attempting to use the modulus operator (%) with non integer operands is a
- 45. Arithmetic (Cont.) Straight-line form Required for arithmetic expressions in C++ All constants, variables and operators appear
- 46. Fig. 2.9 | Arithmetic operators.
- 47. 2.6 Arithmetic (Cont.) Rules of operator precedence Operators in parentheses evaluated first Nested/embedded parentheses Operators in
- 48. Common Programming Error 2.4 Some programming languages use operators ** or ^ to represent exponentiation. C++
- 49. Fig. 2.11 | Order in which a second-degree polynomial is evaluated.
- 50. Decision Making: Equality and Relational Operators Condition Expression can be either true or false Can be
- 51. Fig. 2.12 | Equality and relational operators.
- 52. Common Programming Error 2.5 A syntax error will occur if any of the operators ==, !=,
- 53. Common Programming Error Reversing the order of the pair of symbols in any of the operators
- 54. Outline fig02_13.cpp (1 of 2) using declarations eliminate need for std:: prefix Can write cout and
- 55. Outline fig02_13.cpp (2 of 2) fig02_13.cpp output (1 of 3) (2 of 3) (3 of 3)
- 57. Скачать презентацию