Lab 17: Classes Part 1

Mar 19, 2024  │  m. Mar 18, 2024 by Charlotte Curtis

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.

  1. Create a new file named date.cpp and include your date.h file.

    When defining a member function, be sure to add ClassName:: before the function name to distinguish it from a regular function

  2. Implement 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 your main function and calling the various member functions.

  3. Implement the equal method to compare two Date objects. This should return true if the two objects represent the same date, and false otherwise.

  4. Implement the set method, performing error handling such that it returns false and does not modify the object if any of the day/month/year value(s) are invalid, and returns true 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 return true 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.

  5. Implement the is_leap_year method. This is in the private 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
  6. Implement the days_in_month method. This should return the number of days in the current month, handling leap years appropriately.

  1. 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 main labs 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.

  1. 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).

  2. 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 and mod 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:

OperationDescription
set_balanceset the account balance to a given value
get_balancereturn the account balance
depositupdate the balance by a given value
withdrawupdate 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:



Previous: Lab 16: Linked lists
Next: Lab 18: Classes Part 2