CS 12: Programming Assignment 7

Directory to turnin: as7

Due date: Thursday Dec 7, 2000

Reading:

Dietel & Dietel, Chapter 10: Virtual Functions

Synopsis:

In this assignment, you will be using classes, inheritance, virtual functions, and dynamic memory allocation. You will need your classes from assignment 6. Also, you will need to compile and execute this program under Linux.

Files:

Following is the list of files you will create in this assignment:

Change to the Team, Football, and Baseball Classes

The Team class has a method Display that is overridden by the inherited Football and Baseball classes. Make this method virtual.

The Stack Class

A stack is a list in which items are added to and removed from the top. (Think of a stack of plates at an all-you-can-eat restaurant.) You are going to create a Stack class to hold Football and Baseball objects.

Normally, a C++ program can not store objects of different types in the same data structure. However, inheritance makes this possible. Both Football and Baseball are inherited from Team. Therefore, the Stack class will store pointers to Team objects, but because Football and Baseball objects are also Team objects, this means the Stack class can also contain pointers to Football and Baseball objects (though they will be treated as Team objects).

The class definition for Stack has been provided for you. Click here to get it. You should not need to modify this file.

The class should implement the Stack using a linked list. This will be discussed in lecture.

Your code for the class member functions should be placed in stack.cc. You will need to add code for the following functions:

bool push( Team *team )

This method takes a pointer to a Team as a parameter and adds it to the top of the stack. The method will need to dynamically allocate a new Node. If this fails, the method should return false. Otherwise, the Team should be added to the Node, the Node added to the linked list, and true returned.

Team *pop()

If the stack is empty, this method should return NULL. Otherwise, it should remove the Node from the top of the stack and return the Team it contains. Make sure and delete the Node as well.

int currentSize()

This method should simply return the value of the size variable.

void displayAll()

This method should iterate over all of the Nodes on the stack, starting from either the top or bottom, and call the Display method for the Team contained in each Node. Note that the Display method was made virtual. What will happen because of this?

~Stack()

This destructor should delete all of the Nodes in the linked list to free all of the dynamic memory allocated by this class. Be careful; simply deleting the top and bottom nodes does not ensure all the nodes are deleted.

The Main Program

The main program should create a Stack variable, then present a menu of options to the user:

   F) Add football team to stack
   B) Add baseball team to stack
   R) Remove team from stack
   S) Show current size of stack
   A) Display information for all teams in the stack
   E) Exit
If F is selected, the user should be prompted for the name and season of a football team, a Football object should be dynamically created, and it should be pushed onto the stack. The same should be done for a Baseball object if B is selected.

If R is selected, the team on top of the stack should be popped from the stack. If the stack is empty, a message saying so should be printed, otherwise a message saying a team has been deleted should be displayed. Also, the deleted team's Display method should be called to print information about the deleted team.

If S is selected, the current number of teams in the stack should be printed via a call to currentSize().

If A is selected, information about all of the teams in the stack should be printed via a call to displayAll.

If E is selected, the program should exit.

Compiling and Running the Program

This program must be compiled and executed under Linux. The graders will not attempt to compile using Visual C++.

Note: In past assignments, some students have included .cpp (or .cc) files within other .cpp (or .cc) files. Do not do this. Only header (.h) files should be included in a file.

Include a makefile to compile your program.