This lab will help you practice with 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 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 as would a method to advance to the next month (note that January follows December and begins the cycle anew) or 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, and 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. That means 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 there-of.
Similarly, they'd like to be able to retrieve 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, same. (There may be only one of some methods, but others may need at least two methods to offer all requested functionality.)
(Perhaps if you had a previous library that did stuff with times and/or dates, it might prove useful here?) (Or was the library over here?) (Oh! I know! I saw it right under this...) (Ooo! Here it is!)
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 Output? How can you have two methods called Input? How can your class have four constructors?! It's lunacy! (Hint: O__r_o_di_g.)
Why didn't you need two versions of your method to return the next Month? (Hint: What data type is returned?)
Does your Input method prompt the user? Why should it not?
Does your Output method(s) print anything besides the month number or name/abbrev. (as requested) (even an endl)? Why should it not?
How do you know what display method the programmer desires when your Output method is called (terrible pun/vocabulary clash, isn't it?)? (Hint: bool? enumeration?)
What about the Input method? How can it detect what kind of form the user is using and adapt to it? (Hint: peek ahead at what's coming next from cin's buffer?)
Does your driver program do one test per run or does it allow multiple tests of the class 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 our advanced efficiency techinques to your Month class. This should include not only inline'ing and const-ness to all of your class methods, but also proper use of member initializition lists on constructors!)
Heck, I'll even throw in another (Level 1) to mark your returned class objects appropriately const to keep the caller using chaining from running into extra trouble.
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 math 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 constructor initializer lists? Is there a certain order to the list? Does it go on the prototype or the definition?
How does marking the return type constant help a programmer doing chaining?
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; although it need not). 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 constant...*sigh*).
Make sure that you fill in the vector with month names only once — not during every conversion request!
I'll add another (Level 2) to not add a new data member to your each individual class object to do this...*double take* WHAT?!?!
There are two approaches to this: a static class constant or a static local constant.
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 a helper variable to remember that we were initialized. (Since the only way we have to put data into a vector is to push_[it_]back or subscript...*sigh*) Make this memory variable a static local as well. Initialize it to 'we have not initialized the vector' and then change it to 'we have initialized the vector' when you do so on the first call to the function.
To achieve this same end, you could also make the vector member itself a static constant. But this is ridiculously hard. Maybe just make it a static constant array instead. (This is one thing that you just can't do in C++98 with a vector, it would seem...although it works fine in C++11. *smile*)
Add (Level 3) for making your math and comparison operations actually overloaded operators.
If you did all above options, this lab could be worth as much as (Level 14).