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,
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:

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:

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:

   

You should do the following:

Further Readings:

Important Note about Nested Constructors (and other FAQs)