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


Steps

1.  Define data
The purpose of this example is to create a new data type to maintain stacks. As you might know, a stack is a data structure with a very simple access rule: last in, first out. Data elements stored in a stack should be of the same type (however, the elements can be of a simple standard data type like integer, or they can have a complex data type like pointer to structures). Data elements are stored in a stack in a linear fashion, one by one, and the stack capacity is predefined. Figure 4.1 shows the stack operations.


Figure 4.1  The stack operations.


We are going to start with a stack that handles characters. The stack length will be 256, and will be implemented as an array. This means that our data will be described as an array of characters:
char SStackBuffer[256];
2.  Specify functions according to the operations
The next step will describe the operations that we can perform on the stack and the functions that we are going to implement.
Traditionally we consider a stack to be a data storage structure where we can push the data in or pop the data out. Usually, when we think of pushing the data into the stack, we assume that the other elements are moving to the stack bottom. When we imagine popping an element from the stack, we assume that the other elements are moving to the top. However, this is not a necessary behavior. The only thing we care of when implementing the stack is to follow the last in, first out (LIFO) rule.
To push and pop elements we are going to create two functions, Push and Pop. These functions’ declarations are as follows:
// 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 Push (char c);

// 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 Pop (char& c);

We constructed the stack as an array and declared two functions to support the stack operations. However, the two functions bring up two problems: What should the program do if we try to push an element into the stack and there is no space for it? Also, what should the program do if we try to move the top element out of the empty stack?
These questions result in two more functions, IsEmpty and IsFull:
// The IsEmpty method determines if the stack is empty.
// If yes, it returns 1 (true).
// If no, it returns 0 (false).
   int IsEmpty(void);

// The IsFull method determines if the stack is full.
// If yes, it returns 1 (true).
// If no, it returns 0 (false).
   int IsFull(void);
3.  Create a class
Classes are the core of C++ programming. In the very beginning of C++ history, Bjarne Stroustrup, the author of the language, called C++ “C with classes.” We can consider classes as new data types at least at this stage. Let’s create a class declaration for our stack and learn a little about class components.
// This is a stack class
//

class SStack
{

private:
char SStackBuffer[256];
int CurrentElement;

public:

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

// 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 Push (char c);

// 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 Pop (char& c);

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

};

The preceding code represents a class declaration. This is very similar to structure declarations (and, in C++, classes and structures differ only in default access rights to the data). Let’s see how the class is constructed.
The SStack class specifies data members
char SStackBuffer[256];
int CurrentElement;

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

The main concept behind classes is the combination into one single entity of data and algorithms that process the data. The data items are called data members and the algorithms are called member functions. In other books you can find names such as methods and function members for member functions.
There are two new functions in the class:
SStack(void);     // Constructor
~SStack(void);     // Destructor

SStack() and ~SStack() are the constructor and destructor of the class. The name of the constructor is the same as the name of the class itself. The name of the destructor is the name of the class starting with a tilde (~). The functions play a special role. After an instance of the class is created, the constructor is executed. When a class object finishes its life cycle, the destructor is called. At this point, we are not going to define the functions, we just declared them for future use.
In this class declaration we use two access control keywords: private and public. By default, all data members and member functions are private. However, including the private keyword in the code makes it more readable and structured. Figure 4.2 specifies these important features of classes.


Figure 4.2  Important features of classes.


Now we are ready to answer the question of this section: “How do I hide my data?” The answer is to make the data private and create public functions to access the data. Other programs should know nothing about the implementation of the SStack data type and consider the data type as data storage with a few possible operations on it.
The methodology of hiding data inside classes is called encapsulation. One of the main components of encapsulation is to allow objects direct access only to their own data. The objects’ communication is shown in Figure 4.3.


Figure 4.3  Encapsulation.


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.