C++ Intro
C++ Intro
© 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 produc 1
t or service or otherwise on a password-protected website for classroom
Objectives (1 of 3)
2 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
or otherwise on a password-protected website for classroom
Objectives (2 of 3)
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
or otherwise on a password-protected website for classroom
Objectives (3 of 3)
4 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
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 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
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (1 of 5)
6 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
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (2 of 5)
• Sample Run:
7 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
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (4 of 5)
8 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
or otherwise on a password-protected website for classroom
A Quick Look at a C++ Program (3 of 5)
9 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
or otherwise on a password-protected website for classroom
The Basics of a C++ Program
10 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
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
11 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
or otherwise on a password-protected website for classroom
Comments
12 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
or otherwise on a password-protected website for classroom
Special Symbols
13 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
or otherwise on a password-protected website for classroom
Reserved Words (Keywords)
14 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
or otherwise on a password-protected website for classroom
Identifiers (1 of 2)
15 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
or otherwise on a password-protected website for classroom
Identifiers (2 of 2)
employee Salary
There can be no space between employeeSalary
employee and Salary.
Hello!
The exclamation mark cannot be Hello
used in an identifier.
one+two
The symbol + cannot be used in an onePlusTwo
identifier.
2nd
An identifier cannot begin with a second
digit.
16 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
or otherwise on a password-protected website for classroom
Whitespaces
17 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
or otherwise on a password-protected website for classroom
Simple Data Types (1 of 2)
18 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
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
19 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
or otherwise on a password-protected website for classroom
Data Types
20 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
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)
21 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
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 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
or otherwise on a password-protected website for classroom
char Data Type (1 of 2)
23 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
or otherwise on a password-protected website for classroom
char Data Type (2 of 2)
24 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
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
Decimal Number Scientific Notation C++ Floating-Point Notation
25 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
or otherwise on a password-protected website for classroom
Floating-Point Data Types (2 of 3)
26 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
or otherwise on a password-protected website for classroom
Floating-Point Data Types (3 of 3)
27 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
or otherwise on a password-protected website for classroom
Data Types, Variables, and Assignment Statements
28 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
or otherwise on a password-protected website for classroom
Arithmetic Operators, Operator Precedence, and Expressions (1 of 2)
29 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
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 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
or otherwise on a password-protected website for classroom
Order of Precedence
31 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
or otherwise on a password-protected website for classroom
Expressions
32 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
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 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
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 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
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 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
or otherwise on a password-protected website for classroom
Type Conversion (Casting) (2 of 2)
36 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
or otherwise on a password-protected website for classroom
string Type
37 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
or otherwise on a password-protected website for classroom
Variables, Assignment Statements, and Input Statements
38 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
or otherwise on a password-protected website for classroom
Allocating Memory with Constants and Variables (1 of2)
39 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
or otherwise on a password-protected website for classroom
Allocating Memory with Constants and Variables (2 of 2)
40 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
or otherwise on a password-protected website for classroom
Putting Data into Variables
41 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
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 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
or otherwise on a password-protected website for classroom
Assignment Statement (2 of 4)
43 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
or otherwise on a password-protected website for classroom
Assignment Statement (3 of 4)
44 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
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 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
or otherwise on a password-protected website for classroom
Saving and Using the Value of an Expression
46 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
or otherwise on a password-protected website for classroom
Declaring and Initializing Variables
47 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
or otherwise on a password-protected website for classroom
Input (Read) Statement (1 of 3)
48 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
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 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
or otherwise on a password-protected website for classroom
Input (Read) Statement (3 of 3)
50 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
or otherwise on a password-protected website for classroom
Increment and Decrement Operators
x = 5; x = 5;
y = ++x; y = x++;
51 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
or otherwise on a password-protected website for classroom
Output (1 of 4)
52 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
or otherwise on a password-protected website for classroom
Output (2 of 4)
53 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
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 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
or otherwise on a password-protected website for classroom
Output (4 of 4)
55 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
or otherwise on a password-protected website for classroom
Preprocessor Directives (1 of 2)
56 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
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 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
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 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
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 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
or otherwise on a password-protected website for classroom
Creating a C++ Program (1 of 3)
60 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
or otherwise on a password-protected website for classroom
Creating a C++ Program (2 of 3)
61 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
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 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
or otherwise on a password-protected website for classroom
Debugging: Understanding and Fixing Syntax Errors (1 of 2)
63 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
or otherwise on a password-protected website for classroom
Debugging: Understanding and Fixing Syntax Errors (2 of 2)
64 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
or otherwise on a password-protected website for classroom
Program Style and Form: Syntax
y = w + x; //Line 4
65 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
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 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
or otherwise on a password-protected website for classroom
Use of Semicolons, Brackets, and Commas
67 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
or otherwise on a password-protected website for classroom
Semantics
68 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
or otherwise on a password-protected website for classroom
Naming Identifiers
69 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
or otherwise on a password-protected website for classroom
Prompt Lines
70 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
or otherwise on a password-protected website for classroom
Documentation
71 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
or otherwise on a password-protected website for classroom
Form and Style
72 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
or otherwise on a password-protected website for classroom
More on Assignment Statements
73 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
or otherwise on a password-protected website for classroom
Quick Review (1 of 3)
74 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
or otherwise on a password-protected website for classroom
Quick Review (2 of 3)
75 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
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 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
or otherwise on a password-protected website for classroom