. . . . . . . . . ( end of section Prepare) <<Contents | End>>
Deliver a Question on Vectors or Classes
Exercise -- Mock Quiz on Vectors
[ mock.cpp ]
Questions and Answer
v.clear() removes every item from v. They are destroyed!
After clearing a vector v.empty() is true.
Are arrays still use in C++
yes. Any time we want speed or when we know how many items
we need before we start programming. They are exactly like the
arrays in C.
How are strings and arrays related
They both have a numbered set of items 0,1,2,3,... They are
a fixed number of items. C Strings are arrays of chars
with a special '\0' at the end -- a sentinel. Modern C++
<string> strings can grow and collapse and have lots of
special functions. Hidden inside string is some clever work
with fixed blocks of storage that lets us think we can make
strings bigger and smaller.
How do Multidimensional arrays work
Later! But.... they are arrays of arrays. Or vectors
of vectors.
Why does v[0] and pop_back() crash on an empty vector
Because there is nothing there.
You attempting to access a piece of storage that is no
longer yours.
Which arrays vs strings vs vectors
Arrays for speed and when you know how big they are.
Strings for characters.
Vectors most of the time.
So to store a word use a string. To store many words use
vector <string>.
When to use v[i] vs v.pop_back()
Only use pop-back if you want to remove the last item of data.
Use v[i] to manipulate the i'th item of v.... without removing it.
Can you operate a class inside of a function
Yes.
How many functions in an array
As many as you want.
How many array in a function
As many as you want!
Vectors and strings
Everything you can do to a vector can be done to a string. For
example
s[12]
Similarly
s.push_back('x');
and
s += "x";do the same thing.
The advanced concept of "iterator" works with both vectors and
string in place of subscripts.
Can you push whole words and sentences into a vector
Yes -- in several ways. This may be the one you want:
vector <string> words;
string word;
while(cin>>word)
words.push_back(word);
1: credits from the ATM are not immediately available but are added to the total balance until confirmed by a separate application run outside the ATM.
2: The Account does not verify that the Customer has available funds. This responsibility was allocated to the "Withdrawal" class which asks for the availableBalance when the Customer wants to withdraw some money through the ATM.
3: The Account class will need a constructor. These are often omitted in UML diagrams. For testing we will need to create a dummy account. Something like this
Account ( int acctN, int PIN, double aBal, double tBal);will fit set up the attributes in the class diagram.
In test first development or test driven development we start by writing a test for the class. Something like this: [ test.Account.cpp ] We don't change this until it compiles and runs. To make it compile we need another file that defines the account class: [ Account.cpp ] We repeatedly edit the class, and rerun the test...
Each time we do the
"Simplest thing that can possibly work". As a result we rapidly end up
with a lean and clean implementation of the design.
Exercises
Next -- arrays and vectors
[ 18.html ]
[ lab09/ ]
(vectors)
SOTE
In CS201 you don't need to worry about how it works... just be aware that
you can call a function in the middle of defining it.
6.20 Example Using Recursion: Fibonacci Series
The usual boring example....
6.21 Recursion vs. Iteration
. . . . . . . . . ( end of section Deleted -- Recursion) <<Contents | End>>
Abbreviations