The University of British Columbia
CICS 216
Assignment #1
How Many Days Are There In a Century?
Objectives:
Implementation of a Simple but Useful Class: Date.
Efficient use of nested if-else statements.
Concepts:
Multiple Constructors.
Operator Overloading.
Russell recently graduated from the Computer Science Department of UBC. He is now searching for a job.
Looking through his past, he remembers all the effort he made during his studies. He wonders how many days he has spent in college.
Tasks:
The purpose of this assignment is to design the class "Date" and define operations to handle calendar-based calculations.
We will frequently use this class in the later assignments.
The Date class should contain 3 integers for storing values of day (d), month (m), and year (y).
Another important integer, daysPassed (p), indicates how many days exist between a given date and a reference date, say 1/Jan./1800.
This reference year should be stored in the class as, e.g.,
static const int baseYear 1800;
Given a (d,m,y), the class should be able to compute (and store) the corresponding p, and vice versa.
This class should also handle the variable Month lengths, and more importantly, leap years.
Length of months can be seen here.
In a leap year, Feburary will have an extra day. A leap year occurs every 4 years but not every 100 years, and again every 400 years.
Here is a list of leap years between the years 1800 and 2400.
However, it is not efficient to store this list for detecting the leap years. Instead, we should write the following function,
-
bool isLeapYear(int year)
with some few if-else statements for detecting a leap year. This function returns true if year is a Leap year,
and false otherwise.
We should also provide the following operations in the Date Class:
-
Date(int d,int m,int y)
First initializer (Constructor) gets three arguments (d,m,y), and computes and sets p.
-
Date(int p)
Another initializer (Constructor) gets p and computes and sets (d,m,y).
Note: Since one and only one initializer is called for constructing an object,
each constructor must initialize all variables.
-
string getDate()
Returns the date in "dd/mm/yyyy" format. (For more details about the string object, see the appendix at the end of this page).
-
int getDay()
Returns the day (d). Similar get() methods should be provided for m, y, and p.
Operator Overloading:
It is possible in C++ to overload the operators, such as =,-,+,++,--,==,>=,<=,>,< .
The first operand of these operators is always the object itself, i.e. this .
For example,
we can subtract two dates (and get the number of days between them) with the help of following function:
int operator-(Date &d2)
{
return this->p - d2.p;
}
Similarly, providing these functions can be useful:
- Date operator-(int goBack)
Returns a date which is goBack days before the date stored in this. Note that we can have more thatn one overloads for an operator.
- Date operator+(int goForth)
Returns a date which is goForth days after the date stored in this.
- Date* operator=(Date d2)
Sets all variables in this equal to those in d2. operator= always returns this.
- bool operator==(Date d2)
{
return (this->p == d2.p);
}
Thus, returns true if dates stored in this
and d2 are the same. Please provide all other comparison operators (>=,<=,>,<) in the same way.
Appendix: STL String
We often use the string class
that is defined in the Standard Template Library (STL) that is available
in the C++ environment you use.
In order to use the STL's string class and be able to read in and print
out strings you should include the following two header files
in your program, and use the std namespace:
#include <string>
#include <iostream>
using namespace std ;
A string variable can then be declared using a declaration statement:
string str;
You may initialize a string with a constant string, like
string str("Hello World!");
or with a C style string (i.e. an array of Characters) , as it is shown by
the following statements:
char* cStr = "Hello World!"; // cStr is an array of
char's
string str(cStr); // str is an STL string
When declared, an STL string can:
-
be assigned values using the assignment operator:
str = "Hello World!";
-
get values from the standard input :
cin >> str;
-
be written on the standard output:
cout << str;
-
be concatenated with another STL or C-style string:
str1 += str2;
str1 += "ABCD";
-
be compared with another STL or C -style string:
if (str1 == str2) { ... }
if (str1 < "D") { ... }
You should do the following:
-
Define a C++ class Date that conforms to the specifications stated in
this assignment. Try to add in the header of the class preconditions
and postconditions for all the public functions and the class invariants.
-
Write a C++ test driver that includes all the necessary test cases for the
class. Annotate the output, by printing out enough titles and labels, to
show each test case and the results.
-
For each method in the class you should test the method with a set of
regular values for its parameters and any special values that make the
method to behave differently. For instance when you test the operator+
method you should try to add a value which tests Leap years.
-
You can check the correctness of your algorithms by comparing them with this .
For example, if you have set your reference date to 1800, then the following commands give the number of days
in 19'th century: (36,524 days, as you might have already guessed.)
Date first1900Day = Date(1,1,1900);
int numDaysIn19Century = first1900Day.getDays();
- This test driver must be non-interactive; it should run through all your test cases and output
appropriate comments describing what is being tested.
- Run and test your program.
Further Readings:
Important Note about Nested Constructors (and other FAQs)