Lab 18: Classes Part 2

Mar 21, 2024  β”‚  m. Mar 21, 2024 by Charlotte Curtis

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.

  1. Add a constructor that takes three parameters, one for each of the day, month, and year.
  1. Overload the equality operator (==) to compare two Date objects. In the last lab, I asked for an equal 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 the lab argument:

    $ make lab=classes_p2
    
  1. 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!

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

  1. 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:
    ClassName::ClassName(parameters);
    
    and the implementation:
    ClassName::ClassName(parameters) : member1(val1), member2(val2), ... {
        // constructor code here
    }
    
  2. Add another constructor that takes two parameters, an int for the account id and a double 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.

  3. Does the BankAccount need a destructor? Why or why not? If it needs one, implement it.
  4. Define a main function that creates some test BankAccount objects and calls the various methods to test them. For the purposes of this lab, you can define main in the same file as the class implementation, but in practice you would put it in a separate file.


Previous: Lab 17: Classes Part 1
Next: Lab 19: Designing an ADT