COSC 1320 - C++ Programming
Bob Comer, Professor of Computer Studies


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

Write a constructor function for your class that has 3 parameters that allow the date to be set at the time that a new Date object is created. Each parameter should have a default value. If the month is not specified, it should default to 1 (January). If the day is not specified, it should default to 1. If the year is not specified, it should default to 2001.

Write member functions to print a Date in each of the following formats (3 member functions):

3/15/2010
March 15, 2010
15 March 2010

Your class should also have a private member function that returns the month name as a string. For example, if the date is 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 your textbook, it is best not to do any input or output in member functions. An alternative would be to create print functions to return the formatted date in a string. Then the application program can do the printing. However, converting an integer to a string is tricky, and I really want you to focus on getting your class set up correctly. If you are curious about converting integers to strings, see:

Converting numbers to strings

Validation

When a Date object is created, you should do the following validation.

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.

  • Return to C++ Home Page

    Copyright: ©2011 by the Austin Community College
    Department of Computer Studies. All rights reserved.
    Comments to:
    Bob Comer
    Last updated: February 12, 2011