CPS 6L.2 Ramm - Fall 2001 - 9/21/01 #12
- Announce
- Quiz Today
- Assignment #2 (finally)
5. Iteration with Programs and Classes
- Class Diagram
- Public
- constructor(s)
- member functions
- Private
- Constructors
- The Dice Interface (and Declaration)
dice.h
"Declaration" not code (not Definition)
- The Dice Class Implementation
dice.cpp
- Dice/While Example
roll300.cpp
"Validate by Inspection (???)"
- The Date Class
- Many constructors
- Look at header for interface
date.h
- Monday Blues
mondays.cpp
6. Classes, Iterators, & Patterns
- Class Overview
- The Interface: .h File
- Just including a prototype for functions
- Documentation
- Information Hiding
- Private ( my______ ) Variables
- Private member functions
- Implementation: .cpp File
- Scope of Private Variables
- Global within class
- Avoid arguments/parameters within class
- Not accessible (out of scope) on outside
- The Swap Problem and a Swap Function
- Swap x and y
- Want to use: tmp=x; x=y; y=tmp;
- How do we package this as a functions Swap(a, b)?
- Write Swap
- Reference Parameters
- Arguments NOT copied
- (Value parameters are copied)
- Side effects
- How Do We "return" More Than One Value?
- Reference Parameters vs Value Parameters
- Time: value parameters slower for large arg copy
ref parameters faster
- Space: value parameters require space for arg copy
ref parameters don't require extra space
- Security: Value parameters guaranty no side effects
ref parameters allow changes to be made to outside
- Having Your Cake and Eating it Too
- Const Reference Parameters
- Save time and space
- Still protected
- Stream Iteration: Counting Words
wordcount.cpp
- Input File Streams
-
#include
-
ifstream streamname
-
streamname.open(filename)
-
streamname >> variable
wordcount2.cpp
- UNIX File Streams
stdin, stdout
- Redirection
-
program < infilename
-
program > outfilename
-
program < infilename > outfilename
- Pipes
-
program1 | program2
-
program1 | program2 | program3
- Filters
- Extrema, Statistics
-
stats1.cpp
-
stats2.cpp
- Initialize with real data