CPS 6, Ramm - Summer Semester I - 5/25/00 #6
- Announce
- Lab #3 Today
- Quiz on Monday
Chapter 4: Control, Functions, and Classes
- The
string Member Functions
string demo = "Monday morning";
- Length:
int length()
demo.length() == 14
- Substring:
string substr(int pos, int len)
demo.substr(7,4) == "morn"
- Find:
int find(string s)
demo.find("y m") == 5
(not found: string::npos )
- Examples Using String Functions (class)
- Member Function vs Free Function
-
int length() is member function of string class
-
int sqrt(int) is not part of any class
- String Example: Yes of No Response
- What can go wrong when expecting a yes/no response?
- Look at
yesOrNo.cpp
- What is not tested by this example main()?
- What is a more practical way to use this function?
- Preconditions and Postconditions for Functions
Chapt 5. Iteration with Programs and Classes
- Iteration (pre-tested)
- initialization
- loop test
- loop body
- update
-
while Loop Syntax
while ( test expression )
{
statement list
}
- While Example
blastoff.cpp
- Diamonds are for Fun
diamonds.cpp
- More Looping: Math Tables
formerly got PhD for better tables
pow1.cpp
pow2.cpp
- The Fence Post Problem
-
for Loop Syntax
- for(initialization; test expression; update)
- {
- statement list
- }
- Use where number of iterations is "known"
- Good for most examples shown previously
- Class: Modify
blastoff.cpp
- Problem: Adder Program
- Add an unknown number of numbers
- When to quit
- Sentinel Value
-
while
or for ?
adder.cpp
- Defining Constants
const type identifier = value;
all caps
- Pre-tested and Post-tested Loops
- Iteration (post-tested)
- initialization
- loop body
- update
- loop test
-
do-while Loop Syntax
- do
- {
- statement list
- } while ( test expression )
- Using the
do-while Loop
addwhile.cpp
- Infinite Loops
-
break Statement
- Nested Loops
multable.cpp
nesting can be hidden by using function
- Scope of Variables
- definition
- braces { }
- Private Variables
- Global Variables (XXX)
- Random Numbers & N-sided Dice:
Simulations
- Class:
dice
- Using dice
roll.cpp
- Class Diagram
- Public
- constructor
- member functions
- Private
- Constructors
- The Dice Interface
dice.h
"Declaration" not code (not Definition)
- Dice Implementation
dice.cpp
- Iteration (pre-tested)
- initialization
- loop test
- loop body
- update
- Dice/While Example
roll300.cpp
modify to use for loop