CPS 6L.2 Ramm - Fall 2001 - 9/10/01 #7
- Announce
- PreLab #2
- Assignment #1
- Sample problems for Friday's Quiz
- Last Friday's Quizzes
4. Control, Functions, and Classes
- Functions that Return Values
math.cpp
- Writing
Double Functions
tfuncts.cpp
- Using Functions in Expression
- Can use function returning a value of a certain
type in expressions anywhere you could have used
variable of that type
- Functions can not appears at the left side of assignment
- Mapping Numbers to Strings
Writing string Functions
- Look at
numtoeng.cpp in Book
- Writing Functions
- Design Different "Curving" Functions
- 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