CPS 6.3,4 Ramm - Fall 2000 - 9/25/00 #13
- Announce
- Sample Problems for Friday's Quiz
Chapt 5. Iteration with Programs and Classes
- The Date Class
- Many constructors
- Look at header for interface
date.h
- Monday Blues
mondays.cpp
Chap 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
- Type Casting (coercion)
- Change one type to another
- Only defined where it makes sense
- Several different forms exist
- E.g., changing int to double
- double(n)
- (double) n
- Using Reference Parameters
-
stats3.cpp
-
stats4.cpp