[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 17
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] <17> [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Tue Mar 10 14:14:59 PDT 2009

Contents


    17 Vectors and Classes

      Previous -- advanced Functions

      [ 16.html ]

      Prepare

        Vectors Handout

        Study this [ vectors.html ]

        6.22 Software Engineering Case Study: Identifying Class Operations in the ATM System

        6.23 Wrap-Up

      . . . . . . . . . ( end of section Prepare) <<Contents | End>>

      Deliver a Question on Vectors or Classes

      Exercise -- Mock Quiz on Vectors

      [ mock.cpp ]

      Questions and Answer

        What the difference between v.empty() and v.clear()

        v.empty checks to see if a vector v is empty. It returns true if the vector has no elements. It does not change the vector in any way

        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);

        Why doesn't vector<vector<int>> work

        Because the compiler recognizes the ">>" as the input operator. Can you have arrays of arrays and vectors of vectors Yes.... and vectors of arrays and arrays of vectors..... with out limit.

        What are the special version of vectors

        Any time you write vector<T> you've got a special vector!

        What extra features does a vector have over an array


        1. size()
        2. push_back(a)
        3. pop_back()
        4. empty()
        5. clear()
        6. at(i)

        Where can we use a function like print_backwards

        First make sure you include the prototype before you call it and provide the complete definition before the end of the program.

        When is it good to have vectors of vectors

        When the problem has two dimensional data. Or a sequence of sequences, ....

        What is pop_back()

        It is a function that destroys the last element in a vector.

      Demonstration if we have time -- Test Driven Development of class Account

      We will develop tested code for the following class in the ATM system

      [Class Account from page 306 of Deitel]


        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

    Deleted -- Recursion

      6.19 Recursion

      This is an advanced idea.... and one that is know harder to use than any other looping mechanism. The compiler and the computer do all the heavy lifting to make the computer follow the program you write.

      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

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. Function::programming=A selfcontained and named piece of program that knows how to do something.
  3. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  4. KDE::="Kommon Desktop Environment".
  5. OOP::="Object-Oriented Programming", Current paradigm for programming.
  6. Semantics::=Rules determining the meaning of correct statements in a language.
  7. SP::="Structured Programming", a previous paradigm for programming.
  8. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  9. Q::software="A program I wrote to make software easier to develop",
  10. TBA::="To Be Announced", something I should do.
  11. TBD::="To Be Done", something you have to do.
  12. UML::="Unified Modeling Language".
  13. void::C++Keyword="Indicates a function that has no return".

End