Setup
This lab is a continuation of the previous lab. The only new starter code is the test code and the makefile
in classes_p2
. You will need to copy the date.h
, date.cpp
, and main.cpp
files from the previous lab into the classes_p2
directory.
If you need a refresher on how to move files around in linux, refer way back to Lab 1 .
Main Exercise: Extending the Date
class
The Date
class from the previous lab has a decent amount of functionality, but it’s missing a few things. Make sure to test your class as you go along by defining some instances in your main
function and calling the various member functions.
- Add a constructor that takes three parameters, one for each of the day, month, and year.
Overload the equality operator (
==
) to compare twoDate
objects. In the last lab, I asked for anequal
function that returned a boolean - in this lab, you basically just need to rename it as an operator and it should perform the same task. The calling code then changes from (for example):Date today(21, 3, 2024); Date tomorrow(22, 3, 2024); if (today.equal(tomorrow)) ...
to:
if (today == tomorrow) ...
Identical functionality, but now our
Date
class is behaving a lot more like a built-in data type.This is the tested function for this lab, but again, don’t skip the constructor! The test is the usual
make
command with the directory name as thelab
argument:$ make lab=classes_p2
- Overload the rest of the comparison operators (
!=
,<
,>
,<=
,>=
).Hint: you don’t need to write the logic from scratch for each operator - One operator can call a previously implemented one. Remember your boolean logic!
- Which other operators would be useful for this class? Pick at least one and implement it (note that
<<
requires a bit more extra syntax than the others, we’ll talk about it next lecture).
Extra: Implementing a BankAccount
class
In the last lab
, you specified a BankAccount
class. Now, implement the class in the file bank_account.cpp
. You will need to include the bank_account.h
file and implement the functionality specified in the previous lab. In addition:
- Add a constructor that takes a single
int
parameter and sets an account id to that value. Constructors are public member functions with the signature:and the implementation:ClassName::ClassName(parameters);
ClassName::ClassName(parameters) : member1(val1), member2(val2), ... { // constructor code here }
- Add another constructor that takes two parameters, an
int
for the account id and adouble
for the initial balance.It’d probably be a good idea to have an account ID generator of some sort that ensured uniqueness… but we’ll ignore that for now.
- Does the
BankAccount
need a destructor? Why or why not? If it needs one, implement it. - Define a
main
function that creates some testBankAccount
objects and calls the various methods to test them. For the purposes of this lab, you can definemain
in the same file as the class implementation, but in practice you would put it in a separate file.