Professional Documents
Culture Documents
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 1
product or service or otherwise on a password-protected website for classroom
Objectives (1 of 3)
2
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 2
or otherwise on a password-protected website for classroom
Objectives (2 of 3)
3
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 3
or otherwise on a password-protected website for classroom
Objectives (3 of 3)
4
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 4
or otherwise on a password-protected website for classroom
Introduction
• Computer program
• A sequence of statements whose objective is to accomplish a task
• Programming
• The process of planning and creating a program
• Real-world analogy: a recipe for cooking
5
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 5
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (1 of 5)
6
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 6
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (2 of 5)
• Sample Run:
7
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 7
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (3 of 5)
8
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 8
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (4 of 5)
9
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 9
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (5 of 5)
FIGURE 2-4 Memory spaces after the statement length = 6.0; executes
10
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 10
or otherwise on a password-protected website for classroom
The Basics of a C++ Program
11
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 11
or otherwise on a password-protected website for classroom
Comments
12
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 12
or otherwise on a password-protected website for classroom
Special Symbols
13
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 13
or otherwise on a password-protected website for classroom
Reserved Words (Keywords)
14
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 14
or otherwise on a password-protected website for classroom
Identifiers (1 of 2)
15
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 15
or otherwise on a password-protected website for classroom
Identifiers (2 of 2)
16
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 16
or otherwise on a password-protected website for classroom
Whitespaces
17
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 17
or otherwise on a password-protected website for classroom
Data Types
18
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 18
or otherwise on a password-protected website for classroom
Simple Data Types (1 of 2)
19
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 19
or otherwise on a password-protected website for classroom
Simple Data Types (2 of 2)
TABLE 2-2 Values and Memory Allocation for Simple Data Types
Data Type Values Storage (in bytes)
int –147483648 (= –231 ) to 2147483647 (= 231 – 1) 4
bool true and false 1
char –128 (= –27 ) to 127 (= 27 – 1) 1
–9223372036854775808 (–263 ) to
long long 64
9223372036854775807(263 – 1)
20
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 20
or otherwise on a password-protected website for classroom
int Data Type
• Examples
• -6728
• 0
• 78
• +763
• Positive integers do not require a + sign
• A comma cannot be used within an integer
• Commas are only used for separating items in a list
21
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 21
or otherwise on a password-protected website for classroom
bool Data Type
• bool type
• Two values: true and false
• Purpose: to manipulate logical (Boolean) expressions
• true and false
• Logical values
• bool, true, and false
• Reserved words
22
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 22
or otherwise on a password-protected website for classroom
char Data Type (1 of 2)
23
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 23
or otherwise on a password-protected website for classroom
char Data Type (2 of 2)
24
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 24
or otherwise on a password-protected website for classroom
Floating-Point Data Types (1 of 3)
TABLE 2-3 Examples of Decimal Numbers in Scientific and C11 Floating-Point Notations
25
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 25
or otherwise on a password-protected website for classroom
Floating-Point Data Types (2 of 3)
26
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 26
or otherwise on a password-protected website for classroom
Floating-Point Data Types (3 of 3)
27
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 27
or otherwise on a password-protected website for classroom
Data Types, Variables, and Assignment Statements
28
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 28
or otherwise on a password-protected website for classroom
Arithmetic Operators, Operator Precedence, and Expressions (1 of 2)
29
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 29
or otherwise on a password-protected website for classroom
Arithmetic Operators, Operator Precedence, and Expressions (2 of 2)
• When you use / with integral data types, the integral result is truncated (no
rounding)
• Arithmetic expressions contain values and arithmetic operators
• Operands are the numbers appearing in the expressions
• Operators can be unary (one operand) or binary (two operands)
30
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 30
or otherwise on a password-protected website for classroom
Order of Precedence
31
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 31
or otherwise on a password-protected website for classroom
Expressions
32
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 32
or otherwise on a password-protected website for classroom
Mixed Expressions (1 of 2)
• Mixed expression
• Has operands of different data types
• Contains integers and floating-point
• Examples of mixed expressions
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
33
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 33
or otherwise on a password-protected website for classroom
Mixed Expressions (2 of 2)
• Evaluation rules
• If operator has same types of operands
- The operator is evaluated according to the type of the operands
• If operator has both types of operands
- Integer is changed to floating-point
- Operator is evaluated
- Result is floating-point
• Entire expression is evaluated according to precedence rules
34
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 34
or otherwise on a password-protected website for classroom
Type Conversion (Casting) (1 of 2)
• Implicit type coercion: when the value of one type is automatically changed to
another type
• Cast operator (also called type conversion or type casting): provides explicit
type conversion
• static_cast<dataTypeName>(expression)
35
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 35
or otherwise on a password-protected website for classroom
Type Conversion (Casting) (2 of 2)
36
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 36
or otherwise on a password-protected website for classroom
string Type
37
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 37
or otherwise on a password-protected website for classroom
Variables, Assignment Statements, and Input Statements
38
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 38
or otherwise on a password-protected website for classroom
Allocating Memory with Constants and Variables (1 of2)
39
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 39
or otherwise on a password-protected website for classroom
Allocating Memory with Constants and Variables (2 of 2)
40
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 40
or otherwise on a password-protected website for classroom
Putting Data into Variables
41
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 41
or otherwise on a password-protected website for classroom
Assignment Statement (1 of 4)
• Expression is evaluated and its value is assigned to the variable on the left side
• A variable is said to be initialized the first time a value is placed into it
• In C++, = is called the assignment operator
42
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 42
or otherwise on a password-protected website for classroom
Assignment Statement (2 of 4)
43
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 43
or otherwise on a password-protected website for classroom
Assignment Statement (3 of 4)
44
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 44
or otherwise on a password-protected website for classroom
Assignment Statement (4 of 4)
• Given int variables x, y, and z. How is this legal C++ statement evaluated?
x = y = z
• The assignment operator is evaluated from right to left
• The associativity of the assignment operator is from right to left
45
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 45
or otherwise on a password-protected website for classroom
Saving and Using the Value of an Expression
46
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 46
or otherwise on a password-protected website for classroom
Declaring and Initializing Variables
47
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 47
or otherwise on a password-protected website for classroom
Input (Read) Statement (1 of 3)
48
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 48
or otherwise on a password-protected website for classroom
Input (Read) Statement (2 of 3)
• Using more than one variable in cin allows more than one value to be read at
a time
• Example: if feet and inches are variables of type int, this statement:
cin >> feet >> inches;
• Inputs two integers from the keyboard
• Places them in variables feet and inches respectively
49
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 49
or otherwise on a password-protected website for classroom
Input (Read) Statement (3 of 3)
50
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 50
or otherwise on a password-protected website for classroom
Increment and Decrement Operators
x = 5; x = 5;
y = ++x; y = x++;
51
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 51
or otherwise on a password-protected website for classroom
Output (1 of 4)
52
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 52
or otherwise on a password-protected website for classroom
Output (2 of 4)
53
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 53
or otherwise on a password-protected website for classroom
Output (3 of 4)
Output:
Hello there.My name is James.
Output :
Hello there.
My name is James.
54
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 54
or otherwise on a password-protected website for classroom
Output (4 of 4)
55
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 55
or otherwise on a password-protected website for classroom
Preprocessor Directives (1 of 2)
56
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 56
or otherwise on a password-protected website for classroom
Preprocessor Directives (2 of 2)
• For example:
#include <iostream>
• Causes the preprocessor to include the header file iostream in the program
• Preprocessor commands are processed before the program goes through the
compiler
57
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 57
or otherwise on a password-protected website for classroom
namespace and Using cin and cout in a Program
• cin and cout are declared in the header file iostream, but within std
namespace
• To use cin and cout in a program, use the following two statements:
#include <iostream>
using namespace std;
58
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 58
or otherwise on a password-protected website for classroom
Using the string Data Type in a Program
• To use the string type, you need to access its definition from the header file
string
• Include the following preprocessor directive:
#include <string>
59
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 59
or otherwise on a password-protected website for classroom
Creating a C++ Program (1 of 3)
60
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 60
or otherwise on a password-protected website for classroom
Creating a C++ Program (2 of 3)
61
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 61
or otherwise on a password-protected website for classroom
Creating a C++ Program (3 of 3)
• The statements enclosed between the curly braces ({ and }) form the body of
the function
• A C++ program contains two types of statements:
• Declaration statements declare things, such as variables
• Executable statements perform calculations, manipulate data, create output, accept
input, etc.
62
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 62
or otherwise on a password-protected website for classroom
Debugging: Understanding and Fixing Syntax Errors (1 of 2)
63
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 63
or otherwise on a password-protected website for classroom
Debugging: Understanding and Fixing Syntax Errors (2 of 2)
64
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 64
or otherwise on a password-protected website for classroom
Program Style and Form: Syntax
y = w + x; //Line 4
65
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 65
or otherwise on a password-protected website for classroom
Use of Blanks
• In C++, you use one or more blanks to separate numbers when data is input
• Blanks are also used to separate reserved words and identifiers from each other
and from other symbols
• Blanks must never appear within a reserved word or identifier
66
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 66
or otherwise on a password-protected website for classroom
Use of Semicolons, Brackets, and Commas
67
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 67
or otherwise on a password-protected website for classroom
Semantics
68
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 68
or otherwise on a password-protected website for classroom
Naming Identifiers
69
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 69
or otherwise on a password-protected website for classroom
Prompt Lines
70
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 70
or otherwise on a password-protected website for classroom
Documentation
71
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 71
or otherwise on a password-protected website for classroom
Form and Style
72
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 72
or otherwise on a password-protected website for classroom
More on Assignment Statements
73
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 73
or otherwise on a password-protected website for classroom
Quick Review (1 of 3)
74
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 74
or otherwise on a password-protected website for classroom
Quick Review (2 of 3)
75
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 75
or otherwise on a password-protected website for classroom
Quick Review (3 of 3)
• Use cin and the stream extraction operator >> to input from the standard
input device
• Use cout and the stream insertion operator << to output to the standard
output device
• Preprocessor commands are processed before the program goes through the
compiler
• A file containing a C++ program usually ends with the extension .cpp
76
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 76
or otherwise on a password-protected website for classroom