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; <-- p.get_color();
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 { superdie.Roll(); numtries++; } while (superdie.Roll() != 6) cout << "Total number of tries: " << numtries << endl; // A different approach: int startrolls = superdie.NumRolls(); do { superdie.Roll(); } while (superdie.Roll() != 6) cout << "Total number of tries: " << superdie.NumRolls() - startrolls << endl;
class VolumeControl { public: 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 private: 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"
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++) { dicevector[i].Roll(); } }
int UFO(vector<int> v) { int pos = 0; while ( (pos < v.size()) && (v[pos] != 0) ) { v[pos] = -1; pos++; } 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
UFO(myvector); 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
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 reference 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