Exercise 4.3
Consider the following global data:
string program_name; string version_stamp; int version_number; int tests_run; int tests_passed;
Write a class to wrap around this data.
Why might we wish to do this? By wrapping these global objects within a class, we encapsulate their direct access within a small set of functions. Moreover, the names of the objects are now hidden behind the scope of the class and cannot clash with other global entities. Because we wish only a single instance of each global object, we declare each one to be a static class member as well as the member functions that access them.
#include <string> using std::string; class globalWrapper { public: static int tests_passed() { return _tests_passed; } static int tests_run() ...
Get Essential C++ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.