Project #2

Menagerie

due Wednesday, Jan 18, 2012

For this project, you will be writing code for a Menagerie. Like the Bookstore example we looked at in class, Menagerie takes a list of animals and condenses them into a list of the names of the animals and their average age.

You have some code to start you off in menagerie.tar.gz. (See the SFTP and zip handout if you're having trouble handling a *.tar.gz file, or ask on Piazza if you still can't figure it out.) In it you'll find Animal_type.h and Animal_type.cpp, both of which have been implemented for you. You'll need to implement Menagerie.cpp, which will consist of a main function that can take input and collapse it.

Also in the zip file are the files input and sample_output. Your Menagerie.cpp should process the input file and produce exactly the sample_output file, at least in its first iteration. Much of the heavy lifting has been done for you in the Animal_type definitions. To get started, you should take a look through the Animal_type definitions to understand what operations are available. The easiest thing to do may be to start taking a look at Animal_type.h, since the header file has the most compact representation of what the class can do.

What gets turned in: You need to create a zip file that contains a README file, your Menagerie.cpp file and your Animal_type.cpp file. The README should contain answers to the comprehension questions below. You should also submit a file containing your own set of input, listing a set of animals including the type of animal, their name and age. Your input file should contain at least 5 animals total. As usual, you should submit your zipped project on Blackboard.

Testing your code: Once you've written a Menagerie.cpp, you should be able to test your code using the following commands:

$ g++ -o menagerie Menagerie.cpp Animal_type.cpp
$ ./menagerie < input
Dog     Tom, Harry      7
Parrot  Kiki    3
Cat     Snowball, Lila, Joseph  8.66667

At the top of your README, put a header containing your name, the project name, and the date. Number the answers to your questions. Feel free to get as creative as you like, but if you make any major changes, please note them in your README.


    Comprehending Animal_type

  1. Take a look at the header file for the Animal_type class. Look at the private data members. What are the private data members? Where does each one first get initialized? What's the purpose of each of these variables?

  2. What are the possible constructors for the Animal_type class? What's the difference between them?

  3. Look at the parameter lists for the operators operator>> and operator<<. In plain English, what are the types of the parameters? Why?
  4. Now look at the implementation in the Animal_type.cpp file. This looks similar to the Bookstore.cpp code we wrote in class, but has a few key differences, mostly having to do with the fact that the Animal_type code keeps track of a vector of strings in order to hold on to the names of all the animals of a certain species. Peruse the code and answer the following in your README.

  5. The implementation for operator!= says !(lhs == rhs). Normally in Boolean logic, saying !(A == B) is equivalent to saying (A != B). Why did we use this syntax?

  6. Why did we define operator+ in terms of operator+=, instead of the other way around?

  7. Why do the input and output operators (>> and << respectively) have a return type that isn't void? What does this allow you to do? (Think of the lines we sometimes write that involve multiple uses of an operator on a single line.)
  8. In our Bookstore code for the input operator operator>>, we only sometimes used the default constructor to re-initialize the Sales_item variable. Why does this code always re-initialize a?
  9. Writing Menagerie.cpp

  10. Write a very simple version of Menagerie.cpp that will prompt the user to input a single animal, its name and age, and then print out the same information. Make sure it compiles:
    $ g++ -o menagerie Menagerie.cpp Animal_type.cpp
    $ ./menagerie
    Don't turn in your code yet.

  11. Basing your code off of the code in Bookstore.cpp, write a full Menagerie.cpp that can handle the input file available in the tarball. Be sure that your output matches the sample_output file exactly. What changes did you have to make from the Bookstore code? Why?

  12. We used a const_iterator in the definitions for both operator+= and operator<< in order to iterate through all the names in an Animal_type. Why did we use const_iterator over iterator? Try changing the code to read just iterator - what happens when you try to compile it?

    Fix the issue before you move on to the next question.

  13. The landlady for the menagerie is irritable and wants to make sure that in her report, she can make sure none of the animals in the menagerie are over 12 years old. To prove it, she wants to see a report that contains not just the average age of all the animals of a type, but also the individual ages for the animals.
    1. Modify the Animal_type code to accomodate her request. Think about what sort of data type you need to store the ages, and how you will know which age goes with which animal.

    2. Start by looking at the private data members of the Animal_type class. Is there room in Animal_type to store specific age information? Add a variable that can store this data.

    3. It won't be enough just to be able to store the ages, you need to make sure that the operators on Animal_type will correctly modify the data member for the ages.

    4. Make sure that the report the menagerie executable produces now includes the ages next to the names of the animals, so that the first line of the output using the input file included should look like this:
      Dog    Tom-9,Harry-5    7