review chapter 1 2-3
TRANSCRIPT
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
Review
Chapter 1, 2, and 3
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
The Problem Analysis–Coding–Execution Cycle
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 3
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
Processing a C++ Program (cont'd.)
– 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
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
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
Reserved Words (Keywords)
• Reserved words, keywords, or word symbols– Example:
• int• float• double• char• const• void• return
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 8
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 number
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 9
Identifiers (cont'd.)
• Legal identifiers in C++:firstconversionpayRate
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 10
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 bytesbool : true or false (0), one byte.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 11
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 binary
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 12
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 )) + 6
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 13
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.5
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 14
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
Type Conversion (cont'd.)
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 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 13
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 17
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 word
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 18
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?
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 19
x = 5;y = ++x;
x = 5;y = x++;
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
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 output
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 21
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 screen
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 22
Output (cont'd.)
• A manipulator is used to format the output– Example: endl causes insertion point to
move to beginning of next line
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 23
Output (cont'd.)
• 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
Output (cont'd.)
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 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
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 miles
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 27
Input (Read) Statement (cont'd.)
• 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
respectively
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 28
cin and the Extraction Operator >> (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 29
cin and the Extraction Operator >> (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 30
cin and the Extraction Operator >> (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 31
cin and the Extraction Operator >> (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 32
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
Syntax Errors & Logical Errors
• Syntax errors – Reported by the compiler
int x; //Line 1int y //Line 2: errordouble z; //Line 3y = 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 statement
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 34
HW1: Submission next meeting