CSE2305
Object-Oriented Software Engineering
Exercise 2: The Date Class
This exercise is designed to reinforce the concepts discussed in the lectures.
There are no marks for completing this exercise, however your attendance at
tutorials and successful completion is officially recorded and may be used
as
a positive adjunct to assessment in special circumstances.
You are strongly advised to compete the exercise as it will help
you with the concepts discussed in lectures and assist you in successfully
completing
the practical work. Exercises often mysteriously reappear as exam
questions.
Topic: Develop a simple Date class in C++
Purpose: a chance to develop understanding of basic object-oriented
programming using C++.
How to Proceed:
- Programmers often develop a "kit bag" of useful classes that they
find they need for almost every project. Here is your chance to begin your
own collection and learn C++.
- For this exercise, you are to design and implement a simple class whose
purpose is to represent a date.
- Dates are expressed in a number of different formats, e.g. 27/3/02, 27
March 2005, Wednesday, March 27, 2005. Different countries and cultures
use different
formats and ordering for dates (e.g. the North American format is MM/DD/YY
or MM/DD/YYYY). A date entered in one format should work for another (i.e.
the
Date
class should encapsulate the date without
relying on a specific representation).
- Clearly we need to distinguish between internal representation and formats
we can display the date in.
- Remember Y2K? What can we learn from this regarding dates – how far
into the future will your Date class work? How far back in the past?
- Please come to the tutorial with a written design for the methods and data
members of your class. Priority for help will be given to students who have
completed this before the tute begins.
- Make sure your Date class has the following abilities:
- To print the date to a stream (e.g. myDate.print(ostream&
s))
- To return the date as a text string
- To set myself to the current date
- To set the day, month, and year (individually)
- To find out the day of the week
- To find out the day of the year
- To find out if the year is a leap year
- To find out the week of the year
- Optional: to change to output format of the date, locally to the object
and globally to all instances of the class
Hints:
- You may be interested in the UNIX library calls gettimeofday(2),
time(3) and ctime(3).
- Keep your internal representation private and use accessor methods to get
and set components of the date.
- Sophisticated date formats use metacharacters to prototype the format, e.g.
myDate.setFormat("%D/%M/%Y")
so that when the date is printed it follows the specified format (see the
format options for the UNIX date(1)
command as an example). You could try this or alternatively, have a series
of preset formats that are set by index or name, (e.g. myDate.setFormat(aus_date_format)
)
- A nice way to handle things global to a class is to use static
class methods and data members.
This material is part of the CSE2305 – Object-Oriented
Software Engineering course.
Copyright © Jon McCormack, 2005. All rights reserved.
Last modified:
August 5, 2005