Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


4.  Class implementation
To code actual class functions, we have to design the logic of maintaining the stack. Because we decided to use an array for the stack implementation, we are going to change the strategy of actual elements moving inside the stack. Figure 4.4 shows the implementation of the stack.


Figure 4.4  Implementation with arrays.


The following code defines the stack class:
// Class declaration
// file stack.h

class SStack
{

private:

char SStackBuffer[256];
int CurrentElement;

public:

     SStack(void);  // Constructor
     ~SStack(void);  // Destructor

     int Push (char c);
     int Pop (char& c);
     int IsEmpty(void);
     int IsFull(void);
};

The preceding code is the class declaration. It is a well-known practice to separate class declarations in header files. In this example the code is located in the file stack.h.
// Class definition
// file stack.cpp

#include “stack.h”

// Constructor

SStack::SStack(void)
{
    CurrentElement = -1;
}
// Destructor
// is empty yet

SStack::~SStack(void)
{

}

// The IsEmpty method determines if the stack is empty.
// If yes, it returns 1 (true).
// If no, it returns 0 (false).

int SStack::IsEmpty(void)
{
if (CurrentElement < 0) return 1;
else return 0;
}

// The IsFull method determines if the stack is full.
// If yes, it returns 1 (true).
// If no, it returns 0 (false).

int SStack::IsFull(void)
{
   if (CurrentElement >= 256) return 1;
   else return 0;
}

// The Push method pushes the c character into the stack.
// It returns 1 if success.
// It returns 0 if failure (the stack is full).

int SStack::Push (char c)
{
    if (IsFull()) return 0;
    SStackBuffer[++CurrentElement]= c;
    return 1;
}

// The Pop method moves the top character from the stack
// to the c character.
// It returns 1 if success.
// It returns 0 if failure (the stack is empty).int SStack::Pop (char& c)
{
    if (IsEmpty()) return 0;
    c= SStackBuffer[CurrentElement--];
    return 1;
}

The preceding code is the class implementation (or class definition). Note that we used the scope resolution operator (::) to define the member functions in our class.
The SStack constructor initializes the CurrentElement variable. The variable is intended to handle the number of the element most recently pushed into the stack. The variable is initialized with –1 to show that at the very beginning there is no current element in the stack.
The IsFull and IsEmpty functions check whether the CurrentElement is within the limits from 0 to 255. They are executed in Push and Pop functions, respectively.
5.  Test the class
To test that our class works correctly, we are going to write a small program that displays Hello, world! in the reverse order. Please note that we are creating the program in a separate file. Therefore, we included stack.h in the program.
// Test program for the stack class
// the program displays “Hello, world!”
// in the reverse order

#include <iostream.h>#include <string.h>

#include “stack.h”

int main()
{
    unsigned int i;

    SStack TestStack;
    char buffer[80];
    memset (buffer, 0x00, 80);
    strcpy (buffer, “Hello, world!”);

    for (i= 0; i <strlen (buffer); i++)
        TestStack.Push(buffer[i]);

    i= 0;
    while (!TestStack.IsEmpty())
        TestStack.Pop(buffer[i++]);

   cout << buffer << endl;

   return 0;
}

The program simply takes the string Hello, world!, pushes it into the stack, and then moves the stack elements into the buffer in the reverse order. If you run this program the line
!dlrow ,olleH

will appear on the screen.
The main program shows the result of the encapsulation. The program doesn’t know anything about the array implementation of the stack. Because the data is hidden in the class using the private keyword, the main program can’t see the data. Also, because the member functions Pop and Push are the only tools to access the stack (and we took care of the IsFull and IsEmpty situations), no external program can change or destroy the data in the class.
6.  More advantages of encapsulation
I personally don’t like the implementation of the stack that we’ve created. The array that we defined in the class is a good approach if we don’t want to extend the stack. Currently we are limited to 256 elements of our stack. What would we do if we needed a bigger stack? In that case, we would have to change this number and recompile the class. This is too much of a hassle when working with a big project.
Let’s create a more flexible class and change the array to a string with nonfixed length. The actual length will be passed from the main program as a parameter to the class constructor.
// Class declaration
// file stack.h

class SStack
{

private:

int SStackLength;
char* SStackBuffer;
int CurrentElement;

public:

   SStack(int IniLength=256);     // Constructor
  ~SStack(void);               // Destructor

   int Push (char c);
   int Pop (char& c);
   int IsEmpty(void);
   int IsFull(void);

};

In the class declaration we changed the data section. We added one more variable that will represent the length of the stack and changed the array to the pointer to a string of characters.
In the function section we changed the constructor function. It will accept the length of the stack defaulting to 256.
To support the new stack implementation, we have to change the class definition.
// Class definition
// file stack1.cpp

#include “stack1.h”

// Constructor

SStack::SStack(int IniLength)

{
     CurrentElement = -1;
     if (IniLength < 256) SStackLength = 256;
     else SStackLength = IniLength;
     SStackBuffer = new char [SStackLength];
}
// Destructor
// is empty yet
SStack::~SStack(void)
{
    delete [] SStackBuffer;
}

// The IsEmpty method determines if the stack is empty.
// If yes, it returns 1 (true).
// If no, it returns 0 (false).

int SStack::IsEmpty(void)
{
if (CurrentElement < 0) return 1;
else return 0;

}

// The IsFull method determines if the stack is full.
//  If yes, it returns 1 (true).
// If no, it returns 0 (false).

int SStack::IsFull(void)
{
    if (CurrentElement >= SStackLength) return 1;
    else return 0;
}

// The Push method pushes the c character into the stack.
// It returns 1 if success.
// It returns 0 if failure (the stack is full).

int SStack::Push (char c)
{
   if (IsFull()) return 0;
   *SStackBuffer++= c;
   CurrentElement++;
   return 1;
}

// The Pop method moves the top character from the stack
// to the c character.
// It returns 1 if success.
// It returns 0 if failure (the stack is empty).
int SStack::Pop (char& c)
{
   if (IsEmpty()) return 0;
   c= *--SStackBuffer;
   CurrentElement--;
   return 1;
}

The most important changes in the class were made to create the buffer dynamically in the constructor using the new operator and destroy the buffer in the destructor using the delete operator.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.