CPS 6, Ramm - Spring 2000 - 2/4/00 #8
Chapter 2: C++ Programs: Form and Function
- Look at
numtoeng.cpp in Book
- 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 )
- 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
-
for Loop Syntax
for ( initialization; test expression; update
)
{
statement list
}
- 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 )