There’s a fair bit going on in this lab, so don’t leave it to the last minute! I promise the next one is much simpler in exchange.
Setup
As usual, cd
to your labs repo and pull the new content. The new lab is in the classes_p1
directory.
Main Exercise: Implementing a Date
class
The directory classes_p1
contains a file named date.h
. This time, the method stubs and member variables have been defined for you. Note that some methods end in const
; we will discuss what this means next lecture, for now just copy it in the implementation.
Create a new file named
date.cpp
and include yourdate.h
file.When defining a member function, be sure to add
ClassName::
before the function name to distinguish it from a regular functionImplement the
print
method in the format of your choosing. You can use this to test your class as you go along by defining some instances in yourmain
function and calling the various member functions.Implement the
equal
method to compare twoDate
objects. This should returntrue
if the two objects represent the same date, andfalse
otherwise.Implement the
set
method, performing error handling such that it returnsfalse
and does not modify the object if any of the day/month/year value(s) are invalid, and returnstrue
and sets the values appropriately if they are valid.hint: you’ll probably want to call your
days_in_month
method to help with this! For now you can just set the values directly and returntrue
no matter what, then add the error handling later. This is a variation of a function stub - implementing minimum functionality to get the program to compile and run, and then add the rest later.Implement the
is_leap_year
method. This is in theprivate
section, but nothing is different about the implementation - it just means only an instance of the object can call it. A leap year meets one of the following conditions:- Evenly divisible by 400, or
- Evenly divisible by 4 but not 100
Implement the
days_in_month
method. This should return the number of days in the current month, handling leap years appropriately.
Finally, implement the
increment
method. This should add one day to the current date, handling new months/years appropriately. You’ll probably want to call your previous two functions to help with this!This is the tested function for this lab, but you can’t skip over the other parts as you’ll need to call the other member functions inside this one. For example, you’ll need to call
days_in_month
to figure out if you need to increment the month.To run the test for this lab,
cd
up to your mainlabs
directory and run the following command:$ make lab=classes_p1
Extra challenge
Implement the day_of_week
function. Here is some background information that you will need.
The Julian day of a date is a number between 1 and 366 representing the number of days from January 1 to the date in question. Note that January 1 is always Julian day 1. Use the month to count the number of days that have passed from January 1 to the end of the preceding month. Then add the number of days that have passed in the current month. One special case must be handled – the days in February may be 28 (non–leap year) or 29 (leap year).
The day of the week is an integer between 0 (Sunday) and 6 (Saturday). There are many sources that give algorithms for calculating the day of the week. Here is one formula:
day of week = ((year – 1) + julian day + (year – 1) div 4 – (year – 1) div 100 + (year – 1) div 400 ) mod 7
where
div
is integer division andmod
is the modulo operator.
Extra extra exercise: Specifying a BankAccount
class
Create a file named bank_account.h
(not bank_account.cpp
). Define a new class and its specification (the public interface) - you do not need to actually implement the functionality. The class must provide the following public
operations:
Operation | Description |
---|---|
set_balance | set the account balance to a given value |
get_balance | return the account balance |
deposit | update the balance by a given value |
withdraw | update the balance by a given value |
You must decide on the data types and function signature for each method, and define any required private
member variables.
Things to keep in mind:
- Make sure to use the
#ifndef
and#define
guards to prevent multiple inclusions of the header file. - Any member variables should be
private
- you don’t want to allow someone to just set the account balance to whatever they want! - Public-facing member functions should be
public
- these are the methods that users of your class will call.