CS 1005, C Term 2000
Introduction to Programming
Solutions to the 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:
    Arrows are used instead of circles here for typesetting reasons...
    -->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.

       Dice superdie(12);

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

       int thisroll = superdie.Roll();

    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.

       if (superdie.Roll() == superdie.Roll()) {
          cout << "doubles!";
       else {
          cout << "try again";

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

       // One possibility:
       int numtries = 0;
       do {   
       } while (superdie.Roll() != 6)
       cout << "Total number of tries: " << numtries << endl;
       // A different approach:
       int startrolls = superdie.NumRolls();
       do {   
       } while (superdie.Roll() != 6)
       cout << "Total number of tries: " 
            << superdie.NumRolls() - startrolls << endl;

  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.

          VolumeControl();        // constructor
          void TurnUpANotch();    // modifier
          void TurnDownANotch();  // modifier
          void TurnOff();         // modifier
          int  GetVolume() const; // selector
          bool IsOn() const;      // selector

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

       int VolumeControl::GetVolume() const

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

       ok --> VolumeControl xcoordinate;
       illegal --> xcoordinate.my_setting = 1;   // assume that xcoordinate is 
                                                 // of type VolumeControl
       ok --> thisknob.TurnOff();  // assume that thisknob is of type VolumeControl
       illegal --> 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.

       #include "volume_control.h"

  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.

       vector<string> history(20);

    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.

       for (int i=0; i<line.size()-1; i++) {
          cout << line[i] << ",";
       cout << line[line.size()-1] << ".";

    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.

       vector<Dice> dicevector(100);
       for (int i=0; i<100; i++) {
          for (int j=0; j<i; j++) {

  5. Given the function named UFO (Unidentified Function Object) defined below:
       int UFO(vector<int> 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

       answer: 5

    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] << " ";
       Answer: 5
               10 8 6 4 2 0 -2 -4 -6 -8

  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.

    -->value        a local copy of the actual argument must be made when the 
                    function is entered; this is inefficient for large objects
       const reference

    b) State two advantages of using const reference parameters

       the efficiency of a reference parameter (no local copying required)
       the safety of a value parameter (no modification of actual parameters)

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

       when the function in question needs to modify the actual parameter;
       for example, when writing a client program that needs to modify
       the state of an object being passed to it