[ create a new paste ] login | about

Link: http://codepad.org/2fSWW4TM    [ raw code | output | fork ]

C++, pasted on Apr 14:
/*
Problem Definition

In this assignment, you will build a Date class (as date.h and date.cpp), and a test harness (main.cpp) to test it.
File Naming Specifications

The files that you submit should be named main.cpp, date.h, date.cpp. 
The names are case-sensitive.

Specifications

Below is the interface for the Date class: it is our "contract" with you: you have to implement everything it describes, and show us that it works with a test harness that puts it through its paces. The comments in the interface below should be sufficient for you to understand the project (use these comments in your date.h file), 
*/


/* This is the public interface of the class - your contract with the user.

Date( )
default constructor: creates the date 1/1/2000


Date( unsigned m, unsigned d, unsigned y )
parameterized constructor: month number, day, year - e.g. (3, 1, 2010) would construct the date March 1st, 2010 

If any of the arguments are invalid (e.g. 15 for month, or 32 for day) then the constructor must construct instead a valid Date as close as possible to the arguments provided - e.g. in above example the Date would be corrected to 12/31/2010; 
In case of such invalid input, the constructor will issue a console error message: "Invalid date values: Date corrected to 12/31/2010."
(with a newline at the end).

You are required to use the days_per_month helper function in this constructor (see below).


Date( const string &mn, unsigned d, unsigned y )
parameterized constructor: month name, day, year - e.g. (December, 15, 2012) would construct the date December 15th, 2012

If the constructor is unable to recognize the string argument as a valid month name, then it will issue a console error message: "Invalid month name: the Date was set to 1/1/2000" 
with an 'endl' at the end. (Note: This is a not necessarily a good way to handle constructor errors, but until you learn about exceptions it's the best we can do)
If month name is correct but the day is incorrect it should correct it as in other constructor.
Your constructor must recognize both "december" and "December" as month name, but not necessarily "dec" or "Dec" (though you are encouraged to attempt that).


void print_numeric( ) const
accessor function to output a Date exactly in the format "3/1/2012". Do not output a newline (endl) at the end!


void print_alpha( ) const
accessor function to output a Date exactly in the format "March 1, 2012". The first letter of the month names is upper case, and the month name is printed in full - so Jan, january, jan are not valid formats. 
Do not output a newline (endl) at the end!

Private Member Functions

These are "helper" functions - member functions that will never be needed by a user of the class, and so do not belong to the interface (which is why they are "private"). They are, however, needed by the interface functions ("public member functions"), which use them to test the validity of arguments and construct valid dates.

bool is_leap( unsigned ) const
test for leap year. The rule is:
(year % 4 == 0) implies leap year
except (year % 100 == 0) implies NOT leap year
except (year % 400 == 0) implies leap year
So for instance the year 2000 is a leap year, but 1900 is not; 2004, 2008, 2012, 2016, etc. are all leap years

unsigned days_per_month( unsigned m, unsigned y ) const
Returns number of days allowed in a given month e.g. days_per_month(9, 2000) returns 30
It must correctly calculate February's days for leap and non-leap years, and therefore it also needs the year as a parameter.
QUESTION: what will you return if an invalid month number (e.g. 13) is passed in?
NOTE: in this function and the next two, the numbers 1 through 12, the numbers {28, 29, 30, 31}, and the strings "January" through "December" are NOT "magic" numbers or strings)

string name( unsigned m ) const
Returns the name of a given month - e.g. name(12) returns the string "December"

unsigned number( const string &mn ) const
Returns the number of a given named month - e.g. number("March") returns 3

Date add_days( int ) const
Add a number of days to one date to get another: this number may be positive or negative.
Obviously, the new date will have to be a valid date
accounting for the correct number of days in each month, and for leap years.


*/


Output:
1
2
In function `_start':
undefined reference to `main'


Create a new paste based on this one


Comments: