CPSC 427a: Object-Oriented Programming
Michael J. Fischer
October 25, 2011
Modularizing timing tests
The 14-StopWatch demo presented last time illustrated the use of the
StopWatch class to obtain the running time of some simple test
All of the test code was thrown together in a monolithic main()
The code PS2-timing, which will be the basis of problem set 4, puts
some structure on the test code.
It also illustrates some C++ features that we have not used much up to this point.
Structure of class Tester
Some design goals for class Tester:
Tester has two subject member functions:
Tester has a function measureRuntime() that takes a member
function as its argument and returns a HirezTime result.
This requires the use of member function pointers, which will be explained shortly.
Tests are managed by runAllTests() and runOneTest().
Printing is handled by those two functions.
Optional parameters are passed to Tester through its constructor rather than being frozen in the Tester class.
Member function pointers
C++ supports pointers to class member functions through some new constructs:
Declaring member function pointers
The type of a member function must include the type of the implicit
argument as well as the explicit argument types and return type. This is
done by placing ::* between the type of the explicit argument and the
member function name.
declares myPtr to be a pointer to a member function of class MyClass
that takes an explicit int argument and returns a double
If the implicit argument is const, the declaration becomes
double (MyClass::*myPtr) const (int)
Using typedef with member function pointers
Many people find the member function pointer declaration syntax to be
cumbersome and confusing.
I (and many others) highly recommend using typedef to give a simple
descriptive name to the function pointer type.
This new type name is then used to declare function pointer parameters
and function pointer variables.
Creating member function pointers
Function pointers are created using the & operator.
Using member function pointers
Member function pointers are followed using one of the two C++ binary operators .* or ->*.
Assuming myPtr currently points to member function myFun, these calls are equivalent to:
Demo: Hangman Game
Well-known letter-guessing game.
Start with a hidden puzzle word.
Player guesses a letter.
Player wins when puzzle word is uncovered.
Player loses after 7 bad guesses
Overall design Game elements:
Code structure: Model
Code structure: Viewer and controller
Viewer Contained in class Board.
Controller Contained in class Board.
Class Game is a top-level MVC design.
Storage management Two storage management issues in Hangman:
Natural solutions are to store vocabulary as an array of pointers to strings.
Natural way to each string is to use new to allocate a character buffer of the appropriate length.
A StringStore provide an alternative way to store words.
Instead of using new once for each string, allocate a big char array and
copy strings into it.
When no longer needed, ~StringStore() deletes entire array.
Advantages and disadvantages: