CPS 6.3,4 Ramm - Fall 2000 - 9/15/00 #9
Chap 4: Control, Function, and Classes
- 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)