Homework 8

15 points - Due session 9

Goal

The purpose of this assignment is to help you gain experience working with dynamic arrays and build a class in "Orthodox Canonical Form" by defining a constructor, destructor, copy constructor, and assignment operator.

Assignment

Create a class called CIntStack that encapsulates a simple stack of integer values. In addition to a constructor and destructor, the class should provide the methods push() and pop() to add integer values to the stack and remove them from the stack, respectively. The stack should be implemented as a dynamically allocated array of integers. To accomplish this, declare an integer pointer as a member variable of the class. For example:

class CIntStack {
public:
	CIntStack();
	~CIntStack();
	void push(int nVal);
	int pop();
	...
private:
	int* m_pIntArray;
	...
};

To allocate memory for the array dynamically, you use the new keyword, similar to how you would dynamically allocate space for an object. For example:

m_pIntArray = new int[5];  // Allocate space for 5 integers 

At this point, you can index into the array using the usual array syntax. To de-allocate memory for a dynamically allocated array, you use the delete keyword with the following syntax:

delete [] m_pIntArray;

In your CIntStack class, the push() and pop() methods should grow and shrink the underlying array as needed using new and delete as described above. In push(), for example, one effective (although inefficient) strategy would be to create a new array that is one element larger than m_pIntArray, copy the elements from the old array to the new one, delete the old array, and set m_pIntArray to point to the new one. (To keep things simple, you may implement this inefficient strategy and earn full credit on the homework. But try to recognize why copying the contents of the array during each push and/or pop operation is inefficient!)

After you have implemented a class constructor, destructor, and the push() and pop() methods, add a copy constructor and assignment operator to the CIntStack class. Both of these methods should create copies of the stack. The assignment operator must cleanup the memory that is being used by the current object before copying from the source object. (Beware of self-assignment!)

Create a main() function that tests your stack implementation. At a minimum, this function should create a stack object and push and pop a few integer values onto/from it. Also, demonstrate that you can assign one stack object to another and create a new stack object that is a copy of an existing one.

Deliverables

Put the *.h and *.cpp files containing your solution into an archive file named hw7.zip or hw7.tar.Z. Save this archive file in your AISYG 240 course directory (Courses/AISYG 240) on ftp.coe.neu.edu.

Hint

For a code example that uses dynamically allocated arrays and illustrates a class in Orthodox Canonical Form, refer to the "Assignment Operator" sample code accessible from the course home page.

 


This page was last updated 01/03/2002 by Charles Dale.