Title: Date Class
Course: Programming Fundamentals II (TEMPLATE)

COSC 1337 - Programming Fundamentals II

Assignment 4 - Date class

Be sure to read through Chapter 7 Structured Data and Classes before starting this assignment. Your job is to write a class called Date that has integer data members to store the day, month and year. All data members should be private.

Member functions

All member functions should be public unless otherwise noted.

Write a constructor function for your class that has no parameters. The constructor should set the month to 1 (January), the day to 1 and the year to 2001.

Write a "set" member function that allows a new date to be stored in an existing Date object. The function should have 3 integer parameters - one each for the month, day and year. The purpose of these parameters is to provide the values to be stored in the data members of the object. Your set function should do validation checking on the data supplied in the parameters as follows:
- The month should be between 1 and 12 (inclusive).
- The day should be between 1 and 31 (inclusive).
- The year should be between 1950 and 2020 (inclusive).

If one or more of the parameters contains invalid values, do not update any data members of the object. Otherwise, store the information from the parameters into the corresponding data members of the object.

Write three "print" member functions. Each should print a Date in one of the following formats:
3/15/2010
March 15, 2010
15 March 2010
Important: these are class member functions that print the date stored in a Date object.

Your class should also have a private member function that returns the month name as a string. For example, if the Date object holds the date 3/15/2010, this function would return the string "March". The function prototype might look like:

string getMonthName( );

Your print functions that need to display a month name should use this function.

Note: As noted in the textbook, generally it is best not to do any input or output in member functions. One exception is when a member function is designed to perform a specific output operation, such as printing an object in a standard format.

Testing

Write a main function to test your class. Be sure that your output shows that each of your member functions works. And be sure that you try invalid data as well as valid data.

Other Requirements

- Global variables are variables that are declared outside any function. Do not use global variables in your programs. Declare all your variables inside functions.
- Use the C++ string class to represent strings in your program.

© Austin Community College 2016. The content on this page is licensed under a
Creative Commons Attribution 4.0 International License.
Last updated: November 7, 2015