Section 5 - The Complete Listing in Multiple Files

4A.5.1 Multi-File Structure

To help you get used to multi-file projects, which I introduced last week, I'm going to walk you through the process of placing the class prototypes in a separate header file Stack.h, the class method definitions in their own file, Stack.cpp, leaving only the main() in the main .cpp file (which is called Foothill.cpp in my project).

This will help in the next lesson when we derive new classes from these base classes.  It will make our main file less cluttered.

4A.5.2 The Stack.h Header File

Using the method I described in a previous lecture, add a new header file Stack.h to your Header Files folder in the project explorer window.  In brief, right click on Header Files, select Add → New Item ... and select a Header (.h) file type.  Name the file Stack.

Into that file place the prototypes:

// Class StackNode  ----------------------------------
class StackNode
{
public:
  StackNode *next;

  StackNode();
  virtual ~StackNode();
  void show();
};

// Class Stack ---------------------------------------
class Stack
{
  StackNode *top;

public:
  Stack();
  virtual ~Stack();
  void push(StackNode  *newNode);
  StackNode *pop();
  void showStack();
};

4A.5.3 The Stack.cpp Source File

Using the method I described in a previous lecture, add a new source file Stack.cpp to your Source Files folder in the project explorer window.  In brief, right click on Source Files, select Add → New Item ... and select a C++ Source (.cpp) file type.  Name the file Stack.

Into that file place the class method definitions for StackNode and Stack.  You will have to add some include files at the top as indicated:

#include "Stack.h"
#include <iostream>
using namespace std;

// StackNode method definitions --------------------------
StackNode::StackNode()
{
  next = NULL;
}
StackNode::~StackNode()
{
  // nothing needed
}
void StackNode::show()
{
  cout << "(generic node) ";
}
// end StackNode method definitions ---------------------


// Stack method definitions --------------------------
Stack::Stack()
{
  top = NULL;
}

Stack::~Stack()
{
  // don't delete the stack nodes on the stack - they belong to client.
  // nothing to do. If we were cloning nodes in push() then we would need
  // to delete the remainder of the list - but we are not cloning nodes
  // in this class.
}

StackNode *Stack::pop()
{
  StackNode *temp;

  temp = top;
  if (top != NULL)
  {
    top = top->next;
    temp->next = NULL;   // don't give client access to stack!
  }
  return temp;
}

void Stack::push(StackNode  *newNode)
{
  if (newNode == NULL)
    return;   // emergency return
  newNode->next = top;
  top = newNode;
}

void Stack::showStack()
{
  StackNode *p;

  // Display all the nodes in the stack
  for( p = top; p != NULL; p = p->next )
    p->show();
}

// end Stack method definitions ---------------------

4A.5.4 The Main File

Now, in your main file, only leave the main() method and a few include files at the top.  I have more include files than necessary, and you can take out what you don't need, but it doesn't hurt to leave them in.  Your main file will look like this:

#include <iostream>
#include <string>
using namespace std;

#include "Stack.h"

// main method ---------------------------------------
int main()
{
  Stack stk;
  StackNode *p;

  // build the stack
  for (int k = 0; k < 5; k++)
  {
    p = new StackNode();
    stk.push(p);
  }

  // show the stack
  while ( (p = stk.pop()) != NULL)
  {
    p->show();
    delete p;
  }
  cout << endl;
  return 0;
}

Now compile and run your program to make sure it produces the following output:

console shot

You have created a generic stack data structure using linked-list principles.  You have also learned how to derive subclasses from base classes.  In the next lesson, in a couple days, you will put these together to see how we can build stacks of floats, doubles, or even Galaxies without duplicating the stack logic in these base classes.