C++ Programming: From Problem Analysis to        Program Design, Fifth Edition                Review            Chapter 1,...
Processing a C++ Program#include <iostream>using namespace std;int main(){    cout << "My first C++ program." << endl;    ...
The Problem Analysis–Coding–Execution                       CycleC++ Programming: From Problem Analysis to Program Design,...
Processing a C++ Program• To execute a C++ program:      – Step 1: Analyze the problem      – Step 2: Design an algorithm ...
Processing a C++ Program                  (contd.)      – Step 6: Linking:            • Combines object program with other...
A C++ Program#include <iostream>using namespace std;int main(){  int num;  num = 6;  cout << "My first C++ program." << en...
Comments      • Comments used for the reader, not for the        compiler      • Two types:            – Single line      ...
Reserved Words (Keywords)• Reserved words, keywords, or word  symbols      – Example:            •   int            •   fl...
Identifiers• Consist of letters, digits, and the underscore  character (_)• Must begin with a letter or underscore• C++ is...
Identifiers (contd.)• Legal identifiers in C++:      first      conversion      payRateC++ Programming: From Problem An...
Simple Data Types int : integers (numbers without a decimal), four  bytes      long, short, signed, unsigned char : Use...
Arithmetic Operators and Operator            Precedence• C++ arithmetic operators:                 + addition            ...
Order of Precedence• All operations inside of () are evaluated  first• *, /, and % are at the same level of  precedence an...
Expressions• If the two operands are integers            • Yields an integral result            • Example: 5/2  2        ...
Type Conversion (Casting)• Cast operator: provides explicit type  conversion (value of one type is changed  to another typ...
Type Conversion (contd.)C++ Programming: From Problem Analysis to Program Design, Fifth Edition   16
string Type      •    Sequence of zero or more characters      •    Enclosed in double quotation marks      •    Null: a s...
Constants• const: is a memory location whose content  can’t change during execution• The syntax to declare a named constan...
Increment and Decrement                      Operators• Increment operator: increment variable by 1      – Pre-increment: ...
Compound Assignments      +=, -=, *=, /=, and %=• Example:      x *= y;  x = x * y ;     x *= y+2  x= x * (y+2) ;C++ Pro...
I/O Streams• Use iostream header file to extract  (receive) data from keyboard and send  output to the screen      – Conta...
Output      • The syntax of cout and << is:            – Called an output statement      • The stream insertion operator i...
Output (contd.)• A manipulator is used to format the output      – Example: endl causes insertion point to        move to ...
Output (contd.)      • The new line character is n            – May appear anywhere in the string                   cout <...
Output (contd.)C++ Programming: From Problem Analysis to Program Design, Fifth Edition   25
setw• Outputs the value of an expression in  specific columns      – cout << setw(5) << x << endl;• If number of columns e...
Input (Read) Statement• cin is used with >> to gather input• The stream extraction operator is >>• For example, if miles i...
Input (Read) Statement (contd.)• Using more than one variable in cin  allows more than one value to be read at a  time• Fo...
cin and the Extraction Operator >>             (contd.)C++ Programming: From Problem Analysis to Program Design, Fifth Edi...
cin and the Extraction Operator >>             (contd.)C++ Programming: From Problem Analysis to Program Design, Fifth Edi...
cin and the Extraction Operator >>             (contd.)C++ Programming: From Problem Analysis to Program Design, Fifth Edi...
cin and the Extraction Operator >>             (contd.)C++ Programming: From Problem Analysis to Program Design, Fifth Edi...
Input/Output and the string Type         – Entering a char value into an int or           double variable causes serious e...
Syntax Errors & Logical Errors• Syntax errors      – Reported by the compiler            int x;                    //Line ...
HW1: Submission next meeting
Upcoming SlideShare
Loading in …5
×

Review chapter 1 2-3

498 views

Published on

  • Be the first to comment

  • Be the first to like this

Review chapter 1 2-3

  1. 1. C++ Programming: From Problem Analysis to Program Design, Fifth Edition Review Chapter 1, 2, and 3
  2. 2. Processing a C++ Program#include <iostream>using namespace std;int main(){ cout << "My first C++ program." << endl; system("pause"); return 0;}============================================Sample Run:My first C++ program.C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2
  3. 3. The Problem Analysis–Coding–Execution CycleC++ Programming: From Problem Analysis to Program Design, Fifth Edition 3
  4. 4. Processing a C++ Program• To execute a C++ program: – Step 1: Analyze the problem – Step 2: Design an algorithm – Step 3: Create a Program (Coding) • Use an editor to create a source code in C++, Save the program on a secondary storage device (filename.cpp) – Step 4: Directives begin with # are processed by the preprocessor – Step 5: Use the compiler to: • Check that the program obeys the rules • If yes, translate C++ programs into machine language (object code)C++ Programming: From Problem Analysis to Program Design, Fifth Edition 4
  5. 5. Processing a C++ Program (contd.) – Step 6: Linking: • Combines object program with other programs provided by the SDK to create an executable code – Step 7: Loading: • Loads executable program into main memory – Step 8: Execution • CPU executes the program one instruction at a time.C++ Programming: From Problem Analysis to Program Design, Fifth Edition 5
  6. 6. A C++ Program#include <iostream>using namespace std;int main(){ int num; num = 6; cout << "My first C++ program." << endl; cout << "The sum of 2 and 3 = " << 5 << endl; cout << "7 + 8 = " << 7 + 8 << endl; cout << "Num = " << num << endl; return 0;}C++ Programming: From Problem Analysis to Program Design, Fifth Edition 6
  7. 7. Comments • Comments used for the reader, not for the compiler • Two types: – Single line // This is a C++ program. It prints the sentence: // Welcome to C++ Programming. – Multiple line /* You can include comments that can occupy several lines. */C++ Programming: From Problem Analysis to Program Design, Fifth Edition 7
  8. 8. Reserved Words (Keywords)• Reserved words, keywords, or word symbols – Example: • int • float • double • char • const • void • returnC++ Programming: From Problem Analysis to Program Design, Fifth Edition 8
  9. 9. Identifiers• Consist of letters, digits, and the underscore character (_)• Must begin with a letter or underscore• C++ is case sensitive – NUMBER is not the same as numberC++ Programming: From Problem Analysis to Program Design, Fifth Edition 9
  10. 10. Identifiers (contd.)• Legal identifiers in C++: first conversion payRateC++ Programming: From Problem Analysis to Program Design, Fifth Edition 10
  11. 11. Simple Data Types int : integers (numbers without a decimal), four bytes  long, short, signed, unsigned char : Used for characters: letters, digits, and special symbols. Enclosed in single quotes, one byte A, a, 0, *, +, $, &‘, ‘ float: decimal numbers , represents any real number, four bytes double: decimal numbers , represents any real number, eight bytes bool : true or false (0), one byte.C++ Programming: From Problem Analysis to Program Design, Fifth Edition 11
  12. 12. Arithmetic Operators and Operator Precedence• C++ arithmetic operators:  + addition  - subtraction  * multiplication  / division  % modulus operator• +, -, *, and / can be used with integral and floating-point data types• Operators can be unary or binaryC++ Programming: From Problem Analysis to Program Design, Fifth Edition 12
  13. 13. Order of Precedence• All operations inside of () are evaluated first• *, /, and % are at the same level of precedence and are evaluated next• + and – have the same level of precedence and are evaluated last• When operators are on the same level, Performed from left to right• 3 * 7 - 6 + 2 * 5 / 4 + 6 means (((3 * 7) – 6) + ((2 * 5) / 4 )) + 6C++ Programming: From Problem Analysis to Program Design, Fifth Edition 13
  14. 14. Expressions• If the two operands are integers • Yields an integral result • Example: 5/2  2 • If the two operands are floating-point • Yields a floating-point result • Example: 5.0 / 2.0  2.5• If mixed operands • Integer is changed to floating-point • Result is floating-point • Example: 5 / 2.0  2.5C++ Programming: From Problem Analysis to Program Design, Fifth Edition 14
  15. 15. Type Conversion (Casting)• Cast operator: provides explicit type conversion (value of one type is changed to another type) static_cast<dataTypeName>(expression)C++ Programming: From Problem Analysis to Program Design, Fifth Edition 15
  16. 16. Type Conversion (contd.)C++ Programming: From Problem Analysis to Program Design, Fifth Edition 16
  17. 17. string Type • Sequence of zero or more characters • Enclosed in double quotation marks • Null: a string with no characters • Each character has relative position in a string – Position of the first character is 0 • Length of a string : is the number of characters in the string. – Example: length of "William Jacob" is 13C++ Programming: From Problem Analysis to Program Design, Fifth Edition 17
  18. 18. Constants• const: is a memory location whose content can’t change during execution• The syntax to declare a named constant is:• In C++, const is a reserved wordC++ Programming: From Problem Analysis to Program Design, Fifth Edition 18
  19. 19. Increment and Decrement Operators• Increment operator: increment variable by 1 – Pre-increment: ++variable – Post-increment: variable++• Decrement operator: decrement variable by 1 – Pre-decrement: --variable – Post-decrement: variable--• What is the difference between the following? x = 5; x = 5; y = ++x; y = x++;C++ Programming: From Problem Analysis to Program Design, Fifth Edition 19
  20. 20. Compound Assignments +=, -=, *=, /=, and %=• Example: x *= y;  x = x * y ; x *= y+2  x= x * (y+2) ;C++ Programming: From Problem Analysis to Program Design, Fifth Edition 20
  21. 21. I/O Streams• Use iostream header file to extract (receive) data from keyboard and send output to the screen – Contains definitions of two data types: • istream: input stream • ostream: output stream – Has two variables: • cin: stands for common input • cout: stands for common outputC++ Programming: From Problem Analysis to Program Design, Fifth Edition 21
  22. 22. Output • The syntax of cout and << is: – Called an output statement • The stream insertion operator is << • Expression evaluated and its value is printed at the current cursor position on the screenC++ Programming: From Problem Analysis to Program Design, Fifth Edition 22
  23. 23. Output (contd.)• A manipulator is used to format the output – Example: endl causes insertion point to move to beginning of next lineC++ Programming: From Problem Analysis to Program Design, Fifth Edition 23
  24. 24. Output (contd.) • The new line character is n – May appear anywhere in the string cout << "Hello there."; cout << "My name is James."; • Output: Hello there.My name is James. cout << "Hello there.n"; cout << "My name is James."; • Output : Hello there. My name is James.C++ Programming: From Problem Analysis to Program Design, Fifth Edition 24
  25. 25. Output (contd.)C++ Programming: From Problem Analysis to Program Design, Fifth Edition 25
  26. 26. setw• Outputs the value of an expression in specific columns – cout << setw(5) << x << endl;• If number of columns exceeds the number of columns required by the expression – Output of the expression is right-justified – Unused columns to the left are filled with spaces• Must include the header file iomanipC++ Programming: From Problem Analysis to Program Design, Fifth Edition 26
  27. 27. Input (Read) Statement• cin is used with >> to gather input• The stream extraction operator is >>• For example, if miles is a double variable cin >> miles; • Causes computer to get a value of type double • Places it in the variable milesC++ Programming: From Problem Analysis to Program Design, Fifth Edition 27
  28. 28. Input (Read) Statement (contd.)• Using more than one variable in cin allows more than one value to be read at a time• For example, if feet and inches are variables of type int, a statement such as: cin >> feet >> inches; – Inputs two integers from the keyboard – Places them in variables feet and inches respectivelyC++ Programming: From Problem Analysis to Program Design, Fifth Edition 28
  29. 29. cin and the Extraction Operator >> (contd.)C++ Programming: From Problem Analysis to Program Design, Fifth Edition 29
  30. 30. cin and the Extraction Operator >> (contd.)C++ Programming: From Problem Analysis to Program Design, Fifth Edition 30
  31. 31. cin and the Extraction Operator >> (contd.)C++ Programming: From Problem Analysis to Program Design, Fifth Edition 31
  32. 32. cin and the Extraction Operator >> (contd.)C++ Programming: From Problem Analysis to Program Design, Fifth Edition 32
  33. 33. Input/Output and the string Type – Entering a char value into an int or double variable causes serious errors, called input failure – cin can read a string into a variable of the data type string (without spaces) – The function getline() reads until end of the current line (with spaces)C++ Programming: From Problem Analysis to Program Design, Fifth Edition 33
  34. 34. Syntax Errors & Logical Errors• Syntax errors – Reported by the compiler int x; //Line 1 int y //Line 2: error double z; //Line 3 y = w + x; //Line 4: error• Logic errors – Typically not caught by the compiler – Spot and correct using cout statements – Temporarily insert an output statement – Correct problem – Remove output statementC++ Programming: From Problem Analysis to Program Design, Fifth Edition 34
  35. 35. HW1: Submission next meeting

×