Client and Implementer

Most of the time, homeworks are designed so that you are the "implementer" of a piece of software: you are provided a driver, written by the client, and your job is to write or modify classes that work with the given driver. Don't make any changes to the clients code, and dont make any changes to the Makefile.

The Assignment

We're taking the Hookbook social media platform from lab and building on it. Instead of just an ArrayList of Pirates objects, were going to let each Pirate have a bunch of friends. A Pirate can have any number of friends, and it is a one-way relationship: I can be in your friends list, but that doesnt necessarily mean that you are in mine.

Below you'll find the attributes and member functions that youll need to add or modify to our Pirate and ArrayList classes. In some cases, weve specified function name, parameters, and return type, which is important because the driver is already written.

In addition to what we're describing here, you can modify, remove, or add any attributes or member functions that make your classes more robust, as long as the original driver still works. Any accessor functions you add or modify should be declared const.

Make a new struct called PirateMates. This struct should have two attributes: a Pirate object and an ArrayList which represents that Pirate's mates. Define this struct in a separate file called PirateMates.h.

Add the following to the Pirate class:

  • A member function to add a pirate from the file. The Pirate class already has a function to generate a pirate name at random, but we don't want it to be random anymore. We ultimately want to be able to add all the pirates, in order. This function should have name generate_next_pirate, take a reference to an ifstream object as a parameter, and return void. Youll probably want to use getline here, instead of the >> operator, because pirates dont all have the same number of names.
  • Modify the print function to accept a reference to an ostream object. This way, the client can print a pirate to a file, to standard out, to standard error, or anything else we feel like.

In the ArrayList class:

  • Make the Pirate array dynamic. It should be declared as a pointer and allocated to MAX_PIRATES in the constructor.
  • Modify the insert_pirate function. At the moment, it adds the given Pirate to the end of the list, but you'll need to make sure everything is unique, so add the given Pirate only if it doesnt already exist in the list.
  • Modify the delete_pirate function. At the moment, it is just a stub. Modify so that it will look for the given Pirate object, and if it's there shift everything over to the left and decrease the length. This function should work properly whether or not the Pirate object is actually in the Array List. (Dont copy/paste the delete function from the lab starter code, either. That one assumed the Pirate to be deleted was in there.)
  • Modify the print function to accept a reference to an ostream object. This way, the client can print a pirate to a file, to standard out, to standard error, or anything else we feel like. The print function should also put a comma and space between each pirate except for the final one in the array (see the sample output below).
  • Add a destructor. This should delete the dynamic pirates array.
  • Remove the is_full function. In this version, we can have more than MAX_PIRATES pirates.
  • Add a member function to expand the capacity of the array. Remember that MAX_PIRATES represents the initial capacity of the array, and length represents the actual number of Pirate objects in it. In Lab 1, if we added more than MAX_PIRATES to the array, it seg faulted. Now, we want to accommodate as many pirates as possible. When the actual number of Pirates in the array reaches capacity, reallocate the array with twice as much space. E.g., if the initial allocation was for 100 pirates, when you add pirate 101, then this function should:
    • Allocate a new array to hold 200 pirates
    • Print the following to standard out, followed by a linebreak: "Expand to 200"
    • Copy over the contents of the initial array into the new one
    • Delete the initial array to free up the memory
    • Update pirates to point to the newly-allocated array
  • Add a member function to shrink the capacity of the array. When the actual number of Pirates in the array dips below half its capacity, reallocate the array with half as much space. The shrink function should be called at the end of the delete function -- i.e., if the initial allocation was for 100 pirates, you call delete 50 times, have 50 pirates left, and everything is fine. But on the 51st call to delete, you have 49 pirates, so you then call your shrink function which should:
    • Allocate a new array to hold 50 pirates
    • Print the following to standard out, followed by a linebreak: "Contract to 50"
    • Copy over the contents of the initial array into the new one
    • Delete the initial array to free up the memory
    • Update pirates to point to the newly-allocated array
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.