Due Monday, April 8th at 11:59 PM
Overview
Customers (and bosses, and professors) are fickle. They change their minds and requirements just when you think you’ve got a problem solved. In this assignment, you’ll be refactoring your previous assignment to do the same thing, but using an object oriented paradigm.
In object oriented design, data structures and functionality are encapsulated in classes. The client code creates objects and communicates with them via their public interface, but the “how” is completely hidden 1.
For this assignment you will still be implementing a linked list, but your client code (main.cpp) should have no knowledge that you are using a linked list. Instead, you will create an abstract data type (ADT) called Leaderboard, and main will only interact with various Leaderboard objects and their public member functions (aka methods).
Objectives
- To learn about abstract data types.
- To learn about classes and objects.
- To gain experience writing client code which instantiates objects and invokes their methods.
- To gain experience implementing a class.
Program functionality
Your program functionality should be exactly as described in assignment 3 , so that will not be repeated here.
Starter code
Grab the starter code using the following command:
$ git clone /library/students/comp1633/a4.git/
Then run git-asg-config and select a4 to configure your push repository.
The starter code contains the following files:
team.h: As before, this contains theTeamstructure and associated I/O functions, but this time, they’re operators.leaderboard.h: This contains the public interface for theLeaderboardclass. You will need to implement all of the methods in this file, in addition to any private member variables and helper functions you wish to use. Do not modify the public interface.
To provide some more guidance, I will be posting my (imperfect, and certainly not only) solution to assignment 3 after the last possible late day (Friday, March 29) for you to use as a starting point.
main.cpp
Your main function should be very simple. It should perform the functionality described in assignment 3, but it should not contain any linked list code. Instead, it should create Leaderboard objects as needed (passing the appropriate input file stream to the Leaderboard constructor), and then call the appropriate methods on those objects.
Tips:
You should not need to use any pointers in
main.cpp- this means nonewordeleteoperators.When looping through your updates list, use the
length()and[]operator to go through the list as if it were an array. It is the client code’s responsibility to make sure to not index out of bounds.Sample usage might look like:
Leaderboard lb(in_stream); for (int i = 0; i < lb.length(); i++) { Team t = lb[i]; // do something with t }
team.cpp
If you’re happy with your implementation from assignment 3, I would recommend copying this file over and making the necessary changes. All that really needs to be done is changing the read and write functions to behave as operator >> and operator <<, respectively.
The extraction (>>) operator should be nearly identical to the read function. For the write function (now the << operator), the order of parameters and the return value have changed.
leaderboard.cpp
Again, I recommend copying your assignment 3 implementation and starting from there. You will need to add:
- Constructors and destructors
- The
lengthfunction - The
[]and<<operators
In addition, you will need to modify the update, remove, and clear functions to be class member functions, which means you no longer pass the head pointer as a parameter. Instead, you will use a private member variable to keep track of the head of the list.
Tips:
The
[]operator is used to access theTeamat a given index. Since your implementation is a linked list, this will require traversing the list and counting to the correct index, then returning theTeamat that index. Note that since a (const) reference is returned, you can’t return anything if the index is out of bounds, so it is the responsibility of the calling code to make sure the index is valid.We did not specifically cover the
[]operator in class, but it is a binary operator just like==. The function header is provided in theleaderboard.hfile and should not be modified.The
<<operator is much like the previous leaderboard.cpp’swritefunction, but this time it is not a member function so it has no knowledge of theNodestruct. You will need to use thelength()and[]functions to iterate through the list and output eachTeamas in the example above .Technically it doesn’t even need to be a
friendfunction - it could be moved outside the class and it would behave the same way. I was clearly not thinking straight when I wrote these instructions, but consider it an excuse to practicefriendfunctions.The
Leaderboardclass must keep track of the size of the list. You will need to update the size whenever aTeamis added or removed from the list, as well as initialize the size in the constructor and reset it when the list is cleared.The
Leaderboardclass is responsible for managing the memory of theTeamobjects. You will need to implement a destructor to free the memory when theLeaderboardobject goes out of scope. A simple way to do this is to just call your ownclearfunction.To make creating a list from an input stream convenient, a constructor that takes an
istreamreference is provided. This constructor should read from the stream and addTeamobjects to the list (using theTeam’s>>operator) until the end of the stream is reached. This means thatmainshould no longer read from the stream directly.
Marking scheme
This assignment is worth 8% of your final grade and roughly divided as:
- 40% for program functionality (does it behave as specified?)
- 40% for program implementation (is it written as specified?)
- 10% for style and documentation (is it readable and appropriately commented/cited?)
- 10% for incremental development (did you commit your changes regularly with descriptive commit messages?)
Refer to the style guide for a reference on style and documentation. If you use external resources such as Stack Overflow, ChatGPT, or a friend in the class, make sure to cite them in your comments. Failure to cite external resources will be considered plagiarism. An example of a citation is as follows:
// ChatGPT helped me with this function
void foo(int bar) {
// ...
}
If your solution uses vectors or other data structures or techniques not covered in class, you will receive a reduced grade, possibly as low as 0. If you have previous experience, try to challenge yourself to solve this problem using only the basics.
In addition, there will be an automatic 20% deduction if your code fails to compile or run. If the problem is extreme and I cannot fix it with a small change, a grade of 0 may be assigned. Make sure your code compiles and runs on INS with the given makefile before submitting.
Finally, make sure to check your code periodically for memory leaks with valgrind. Memory leaks will incur a penalty of up to 10%, even if the code appears to function perfectly.
Okay, not completely hidden - you can see the private declarations in the class header ↩︎