Similar to last homework, each problem should be separated into its own function and called within main(). Each problem should have its own .h / .cpp file associated with it and include any components needed. Please submit all of your .cpp / .h files when turning in this homework.
Problem 1: a.) A list is a collection of an unordered sequence of elements. C++ has a std::list class that is an Abstract Data Type encapsulating details on how a list is managed. For this problem, you will write your own list implementation. A few constraints on your implementation:
Implement this class using templates where this list can store any type. § All return types will be actual objects and not references to objects. § You may assume that your list will not exceed 10 elements. § You will use an array representing the actual list. Since we know there will be at most 10 elements, you can statically allocate this array. § You will want to keep track of the number of elements in the list using a class variable named size
Your list class implementation should support the following functions: § at(int i) – returns the element at the index i. Throws an exception if there is no element at index i. § empty() – returns true if the list is empty, false otherwise. § first() – returns the element at index 0. Throws an exception if the list is empty. § last() – returns the element at index size – 1. Throws an exception if the list is empty. § insert(T item) – inserts the item at the end of the list. Throws an exception if the list is full. § remove(int i) – removes the element at index i. “Removing” doesn’t mean erasing the contents from memory in this case. You will need to shift all elements inserted after the ith element down 1 in the array in order to prevent any “holes” in the list. Throws an exception if there is no element at index i.
Since this problem requires you to throw exceptions, I recommend defining your own types for each possible exception and using those types when throwing / catching exceptions. These can be defined in the same .h file as your List class. You may consider using the following
InvalidIndexException EmptyListException FullListException
b.) Write test cases in your Problem 1() function that illustrate your list class works as expected. You should test a normal case and the edge case(s) for each function. These test cases will also allow you to practice wrapping function calls in try / catch blocks and handling them appropriately.
Problem 2: a.) Organize and write several classes representing zoo animals. Each animal should have its own class representation that inherits from a base class named “Animal”. All animals have the following common characteristics, which should be defined in an Animal base class (with the appropriate accessor / mutator / constructor):
Each of these classes should have their own accessor / mutator functions for their additional field as well as its own constructor.
These animals were chosen for their variety more than anything else … or else this would be a really boring zoo to visit! To practice polymorphism (late binding), in your Animal class, define a pure virtual function string getSound() that returns a string that the particular Animal type makes. Also, you should define another pure virtual function string getDescription() that returns a string with all of its attributes including the attributes from its base class.
b.) Using your list solution from the previous problem, in your Problem2() function, create an Animal list (i.e. your zoo) and create/insert various animals into this list (at least one animal of each type). Traverse this list and print out the Animal attributes and sounds that each animal makes using your getDescription() function. For example, if you insert a Dog, you can print its attributes inherited from Animal (name, weight, numOfLegs), it’s own distinct attribute (numOfClaws), and the sound it makes (which you define when writing your getSound() function)

Комментариев нет:
Отправить комментарий