CS 1005, C Term 2000
Introduction to Programming
Practice Problems for Test 2

  1. a) Among the following collection of terms, circle the three that correspond to the three basic types of public member functions that are required for most C++ classes:
       Modification member functions     Vector indices
       Loop tests                        Constant member functions    
       Private data members              Constructors             
       Homogeneous functions             Destructors

    b) A class named MyClass has exactly five public member functions, named MyClass(), clear(), paint(), fill(), and get_color(). Assume that p, q, and r are three objects of type MyClass. Circle all statements among those shown below that would be syntactically incorrect if used within a client program of the MyClass class. You may assume that the source file that contains these statements provides access to the MyClass class through the appropriate #include "myclass.h" directive.

       p.clear();                        fill(p);    
       clear(q);                         MyClass z;
       p.q;                              z = MyClass;    

  2. For each of the following, write one or more C++ statements that would accomplish the given objectives within a client program of the Dice class from HW2. All variables used must be explicitly declared.

    a) Declare a 12-sided Dice object named superdie.


    b) Roll the object named superdie and place the result in an object named thisroll.


    c) Print "doubles!" if the results of two consecutive rolls of superdie are the same; otherwise, print "try again". Note that superdie should be rolled exactly two times - no more, and no fewer.


    d) Roll superdie as many times as necessary until a 6 is rolled, and print the total number of tries that were needed. You may not assume anything about how many times superdie has been rolled prior to the start of your program segment (and you should not declare superdie in your segment either).


  3. Consider the VolumeControl class defined below, as it appears in the header file volume_control.h:
       class VolumeControl {
          void TurnUpANotch();    // increases volume to next higher setting
          void TurnDownANotch();  // decreases volume to next lower setting
          void TurnOff();         // sets volume to 0
          int  GetVolume() const; // returns current volume setting
          bool IsOn() const;      // returns true if volume is > 0, false otherwise
          int my_setting;         // current volume setting

    a) List all of the public member functions of the VolumeControl class, and label each as being a constructor, a selector, or a modifier, as appropriate.


    b) Write the header (no body required) of the GetVolume() method exactly as it would appear in the implementation file for the VolumeControl class.


    c) Label each of the following statements as being either allowed or prohibited within a client program of the VolumeControl class.

       VolumeControl xcoordinate;
       xcoordinate.my_setting = 1;   // assume that xcoordinate is 
                                     // of type VolumeControl
       thisknob.TurnOff();  // assume that thisknob is of type VolumeControl
       TurnUpANotch(knob);  // assume knob is of type VolumeControl

    d) Write the include statement that's needed in order to use the VolumeControl class within a client program.


  4. For each of the following, write a program segment that accomplishes the stated objectives using the standard vector class discussed in this course. All variables used must be declared unless otherwise stated.

    a) Declare a vector named history containing 20 objects of type string.


    b) Print the items of a vector named line one by one, separated by commas, with a period after the last item. You may assume that line has previously been declared.


    c) Roll each item of a vector of 100 Dice objects named dicevector as many times as the value of the position index for that item.


  5. Given the function named UFO (Unidentified Function Object) defined below:
       int UFO(vector v)
          int pos = 0;
          while ( (pos < v.size()) && (v[pos] != 0) ) {
             v[pos] = -1;
          return pos;
    Assume that myvector is a vector containing 10 ints, with myvector[i] equal to 10-(2*i) for each i between 0 and 9.

    a) Give the value returned by the call


    b) What output is produced by the following program segment?

       for (int i=0; i<10; i++) {
          myvector[i] = 10 - 2*i;
       int count = UFO(myvector);
       cout << count << endl;
       for (int i=0; i<10; i++) {
          cout << myvector[i] << " ";

  6. a) Which of the following parameter passing modes is usually not a good choice when designing functions that accept large objects of programmer-defined classes as arguments? Explain your answer.

       const reference

    b) State two advantages of using const reference parameters


    c) Describe a situation in which a const reference parameter cannot be used.