Chapter 03 C++
Transcript of Chapter 03 C++
-
8/10/2019 Chapter 03 C++
1/20
INTRODUCTION TOC++
Chapter 03: Expression & Interactivity
1
-
8/10/2019 Chapter 03 C++
2/20
Topics
3.1 The cinObject
3.2 Mathematical Expressions
3.3 Named Constants
3.4 Multiple and Combined Assignment
3.5 Working with Characters and String
Objects
3-2
-
8/10/2019 Chapter 03 C++
3/20
3.1 The cinObject
Standard input object
Like cout, requires iostreamfile
Used to read input from keyboard
Often used with coutto display a user prompt
first
Data is retrieved from cinwith >>
Input data is stored in one or more variables
3-3
-
8/10/2019 Chapter 03 C++
4/20
The cinObject
User input goes from keyboard to the input
buffer, where it is stored as characters
cinconverts the data to the type that matchesthe variable
int height;cout > height;
3-4
-
8/10/2019 Chapter 03 C++
5/20
The cinObject
Can be used to input multiple valuescin >> height >> width;
Multiple values from keyboard must beseparated by spaces or [Enter]
Must press [Enter] after typing last value
Multiple values need not all be of the same type
Order is important; first value entered is storedin first variable, etc.
3-5
-
8/10/2019 Chapter 03 C++
6/20
3.2 Mathematical Expressions
An expression can be a constant, a variable,or a combination of constants and variablescombined with operators
Can create complex expressions usingmultiple mathematical operators
Examples of mathematical expressions:
2 height a + b / c
3-6
-
8/10/2019 Chapter 03 C++
7/20
Using Mathematical Expressions
Can be used in assignment statements, withcout, and in other types of statements
Examples:area =2 * PI * radius;cout
-
8/10/2019 Chapter 03 C++
8/20
Order of Operations
In an expression with > 1 operator, evaluate
in this order
-(unary negation) in order, left to right* /% in order, left to right
+- in order, left to right
In the expression 2 + 2 * 22,
3-8
Do first:
Do last:
Do next:
Evaluate1st
Evaluate2nd
Evaluate3rd
-
8/10/2019 Chapter 03 C++
9/20
Associativity of Operators
-(unary negation) associates right to left
* / % + - all associate left to right
parentheses ()can be used to override theorder of operations
2 + 2 * 2 2 = 4
(2 + 2) * 2
2 = 62 + 2 * (2 2) = 2
(2 + 2) * (2 2) = 0
3-9
-
8/10/2019 Chapter 03 C++
10/20
Algebraic Expressions
Multiplication requires an operator
Area = lw is written asArea = l * w;
There is no exponentiation operator
Area = s2 is written asArea = pow(s, 2);
(note:powrequires the cmathheader file)
Parentheses may be needed to maintain order of
operationsis written as
m = (y2-y1)/(x2-x1);
3-10
12
12
xx
yym
-
8/10/2019 Chapter 03 C++
11/20
3.3 Named Constants
Also called constant variables
Variables whose content cannot be changed
during program execution
Used for representing constant values with
descriptive names
const double TAX_RATE = 0.0675;
const int NUM_STATES = 50;
Often named in uppercase letters
3-11
-
8/10/2019 Chapter 03 C++
12/20
constvs. #define
#define C-style of naming constants
#define NUM_STATES 50
Interpreted by pre-processor rather than compilerDoes not occupy a memory location like a constant
variable defined with const
Instead, causes a text substitution to occur. In
above example, every occurrence in program ofNUM_STATESwill be replaced by 50
3-12
no ;
goes here
-
8/10/2019 Chapter 03 C++
13/20
3.4 Multiple and Combined
Assignment
The assignment operator (=) can be used more
than 1 time in an expression
x = y = z = 5;Associates right to left
x = (y = (z = 5));
3-13
Done Done Done3rd 2nd 1st
-
8/10/2019 Chapter 03 C++
14/20
Combined Assignment
Applies an arithmetic operation to a variable
and assigns the result as the new value of that
variable Operators: += -= *= /= %=
Example:
sum += amt; is short for sum = sum+amt;
3-14
-
8/10/2019 Chapter 03 C++
15/20
More Examples
x += 5; means x = x + 5;
x -= 5; means x = x 5;
x *= 5; means x = x * 5;x /= 5; means x = x / 5;
x %= 5; means x = x % 5;
The right hand side is evaluated before thecombined assignment operation is done.
x *= a + b; means x = x * (a + b);
3-15
-
8/10/2019 Chapter 03 C++
16/20
3.5 Working with Characters and
String Objects
char: holds a single character
string: holds a sequence of characters
Both can be used in assignment statements
Both can be displayed with coutand
-
8/10/2019 Chapter 03 C++
17/20
String Input
Reading in a string object
string str;
cin >> str; // Reads in a string// with no blanks
getline(cin,str);// Reads in a string
// that may contain// blanks
3-17
-
8/10/2019 Chapter 03 C++
18/20
Character Input
Reading in a character
char ch;
cin >> ch; // Reads in any non-blank char
cin.get(ch);// Reads in anychar
cin.ignore();// Skips over next char in// the input buffer
3-18
-
8/10/2019 Chapter 03 C++
19/20
String Operators
= Assigns a value to a stringstring words;words = "Tasty ";
+Joins two strings togetherstring s1 = "hot", s2 = "dog";string food = s1 + s2; // food = "hotdog"
+=Concatenates a string onto the end of another onewords += food;// words now = "Tasty hotdog"
3-19
-
8/10/2019 Chapter 03 C++
20/20
Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 3: Expressions and Interactivity
Starting Out with C++
Early Objects
Seventh Edition
by Tony Gaddis, Judy Walters,and Godfrey Muganda