This lab will help you practice with libraries and classes.
Design and code a class that represents a single month of the year. It should have a single integer-typed data member to represent the month. Make sure to provide for proper construction, access, and mutation for this member as well as input and output methods.
In addition, methods for comparing two months chronologically for ordering would be useful. (You know, to decide if the calling month is earlier, later than, or at the same time as the argument month.)
Also a method to advance to the next month would be handy. (Note that January follows December and begins the cycle anew.) You should also be able to move to a previous month. In fact, thinking about it, it might be good to be able to advance to a month an arbitrary number of whole months in the future or past: January + 5 = June; March - 5 = October.
Place your Month class in a library.
Write a test application (driver) for your class.
Oh, BTW, the programmers who commissioned this class wanted to make sure it could handle not only integer-valued representation of a month but also either whole-word names or three-letter abbreviations for the months. By this they mean that they should be able to construct a Month object from either an integer value or a string — which could be either the whole month name or a 3-letter version thereof. (The Month object would still just store a single integer to represent which month it represented, however.)
Similarly, they'd like to be able to retrieve (i.e. access) the Month's information as either an integer value or a string (of either form), mutate the Month by any of these means, display by any of the three forms, and even allow their user to enter any of the three forms at the console.
But, to keep things as simple as possible, they'd not like a whole lot of different names to remember. Please use the following names for all functions that perform such tasks: set_month, get_month, input, output, advance, before, after, and same. (There may be only one of some methods, but others will need at least two overloads to offer full functionality.)
Do you have any private methods? Do they help support the translation between number and letter/word name representations which occur at several places in this class' methods? Why would such functions be private? Doesn't the user of the class need to call them?
How can you have two methods called set_month? How can your class have four constructors?! This is lunacy! (Hint: O__r_o_di_g.)
Why didn't you need two versions of your method to advance to the next month? (Hint: What data type is returned?)
Does your input method prompt the user? Why shouldn't it?
Does your output method(s) print anything besides the month number or name/abbrev. (as requested) (even an endl)? Why shouldn't it?
How do you know what display method the programmer desires when your output method is called (terrible pun/vocabulary clash, isn't it?)? (Hint: Is a bool enough? Or do you need an enumeration?)
What about the input method? How can it detect what kind of form the user is using and adapt to it?
Does your driver program do one test per run or does it allow multiple tests of the class' features during a single run? Which seems more convenient for you/the end programmer?
Are the tests in your driver program hard-coded/literal or are they adaptable to the needs of the programmer running the tests? Which would be more useful?
Are the tests your driver can run specifically ordered in some way or can the programmer doing the testing choose what s/he is going to test first, next, ... last? Which would be more convenient/useful?
This assignment is (Level 4).
Add (Level 2) to properly apply inline'ing and const-ness to all of your class methods. (This includes proper use of member initializer lists on constructors!)
Should any of your methods be inline'd for speed? Which ones will you inline? How does one do this for a class method?
If you end up inline'ing all of your methods, do you still need an implementation file for your library? Why/Why not?
Do the accessors, output, or specialty operation methods alter the calling object in any way? How do you specify such a situation to the compiler so it can better handle/enforce your decision?
What needs to be in your constructors' member initializer lists? Is there a certain order to the list? Does it go on the prototype or the definition of the constructor?
Add (Level 2) to use a vector to store the month names. Then, instead of using a huge switch, a linear search of the vector could be used to find the proper month — 4 lines instead of 15! (This can actually simplify your class GREATLY.)
Note: This will alter your constructors, mutators, and accessors. It may also affect the input and output methods (when working with names/abbrev.; although it needn't). Note also that this will not add any new accessors or mutators to your class, treat this vector member as if it were constant (even though we can't actually make it a constant member...*sigh*).
Make sure that you fill in the vector with month names only once — not during every conversion request! (Hint: To simplify construction, you may want to do this step in a private helper method...)
However, I'll add another (Level 2) to not add a new data member to your class to do this...*double take* WHAT?!?!
To not add a data member, the vector can be placed into the conversion function(s) as a static local variable. This still affects the storage for your class, but not of each individual object. (A static local variable for a function doesn't get destroyed between calls and is 'initialized' only on the first call to the function. Otherwise, it acts normally and is only 'in scope' within the function.)
To fulfill the single initialization requirement, however, we'll need to remember that we were/n't initialized. (Since the only way we have to put data into a vector is to push_[it_]back or subscript...*sigh*) This could be a separate helper memory variable (another static local as well) or can the vector somehow tell us it was never initialized? Now, on the first call to the function, initialize the vector to the names of the months. But use your memory to not repeat the initialization on later calls.
(To achieve this same end, you could also make the vector member itself static, but that is trickier and makes the process more obvious to programmers using your class.)
If you did all above options, this lab could be worth as much as (Level 10).