CPS 6, Ramm - Summer Semester I - 6/9/99 #14
- Announce
- Assignment #5
- Random Quiz #6
Chap 7. Class Design and Implementation via Simulation
- Initializer Lists - Syntax
- ClassName::ClassName()
- :
- myVar1(argument),
- myVar2(argument),
- ...
- myVarN(argument)
- {// begin constructor body ...
- Structs
- Similarity to Class
- All Public, No Private
- Can Have Member Functions
- An Aggregate of Dissimilar Types
- Example (without member functions)
- struct Student
- {
- string name;
- int height; // height in inches
- double weight; // weight in pounds
- string dob; // date of birth mm/dd/yy
- }
Chap 8. Arrays, Data, & Random Access
- Concepts
- Random Access
- Sequential Access
- Arrays, Vectors
- A Counting Problem
-
gradefreq1.cc
-
gradefreq2.cc
-
gradefreq3.cc
- Vector Vocabulary
- Homogeneous Collection (Aggregate)
- N cells/elements
- index/subscript (0, 1, ..., N-1)
- always start at 0
- last always at N-1
- Vector Creation and Use
- Vector<int> num(6);
- +--+--+--+--+--+--+
- | ?| ?| ?| ?| ?| ?|
- +--+--+--+--+--+--+
- --0--1--2--3--4--5-
-
- num[1] = 21; num[5] = 13;
- +--+--+--+--+--+--+
- | ?|21| ?| ?| ?|13|
- +--+--+--+--+--+--+
- --0--1--2--3--4--5-
-
-
- int k;
- for (k=2; k<6; k++) num[k] = k*k;
- +--+--+--+--+--+--+
- | ?|21| 4| 9|16|25|
- +--+--+--+--+--+--+
-
-
- int j;
- for (j=0; j<6; j++) cout << num[k] << " ";
-
- ? 21 4 9 16 25
Vector Definition Syntax
- Vector<type> name;
- Vector<type> name(expression);
- Vector<type> name(expression, value);
- type is any with default (parameterless) constructor
Vector Initialization
- Use Constructor
-
Vector<int> nums(4, -1);
-
Vector<string> words(12, "Hi");
- Use Fill Member Function
-
Vector<int> nums(4);
-
nums.Fill(-1);
-
Vector<string> words(12);
-
words.Fill("Hi");
- Use Client Program
Vector Parameters
-
letters.cc
- char type
- functions tolower(char), isalpha(char)
- get(char) member function
- fail() member function
A Draft Word in Context Program
-
context.cc
Redesigning a Word in Context Program
Redesign in Classroom
-
context2.cc
How to Solve Problem Without Vectors?