The purpose of this assignment is to learn even more about C++. Specifically, how to managed the files for class templates.
The following is an example of creating a class for an integer stack. The class definitions can go in a file named "stack.h", and the class' member functions can go in a seperate file named "stack.cpp". In short, one file contains the definition and another file contains the implementation.
With these files divided we can compile stack.cpp into an object code file just once. Later, that single .o file can be linked with one or more applications that need an integer stack. For example, if bob.cpp needs an integer stack, bob could just #include stack.h and then link to stack.o without re-compiling stack.cpp.
stack.h
class IntegerStack
{
public:
IntegerStack();
int IsEmpty();
int IsFull();
void Push (int value);
int Pop();
private:
int top;
int items[100];
};
|
Here is a main() to test that integer stack. This main() reads an integer, breaks the integer into individual digits, then prints the digits one per line.
stack_test.cpp
#include <iostream>
using namespace std;
#include "stack.h"
int main()
{
IntegerStack mystack; // the stack
int number; // user input
// get a number
cout << "Enter an integer : ";
cin >> number;
// put the digits of the number on the stack
while (number > 0)
{
if (!mystack.IsFull())
{
mystack.Push(number%10);
number = number / 10;
}
else
cout << "Stack Overflow!!!\n";
}
// print the contents of the stack
cout << "That number printed vertically is\n";
while (!mystack.IsEmpty())
cout << mystack.Pop() << endl;
cout << endl << endl;
}
|
It is very important to note, that we can change this test program file over and over and never need to recompile the stack.cpp file.
For example, the following will not work. We cannot compile the following stack.cpp without knowing what <ItemType> will be.
stack.h
template <class ItemType>
class StackType
{
public:
StackType();
bool IsEmpty();
bool IsFull();
void Push (ItemType value);
ItemType Pop();
private:
int top;
ItemType items[10];
};
|
There are very few solutions to this problem. One solution is put both the class definition and the class' member functions into the same file (using named .h), and then have the client include this one single file.
I made two minor changes to stack_test.cpp so that it will not work. Your assignment is to fix the stack_test.cpp file. Hint: I just deleted two lines. Turn in a corrected version of stack_test.cpp.
stack_template.h
|