13.2 Improved Stack

I l @ ve RuBoard

The structure version of the stack works but has a few drawbacks. The first is that the data and the functions are defined separately, forcing you to pass a struct stack variable into each procedure.

There is also the problem of data protection. The fields data and count are accessible to anyone . The design states that only the stack functions should have access to these fields, but there is nothing to prevent rogue code from modifying them.

A C++ struct is a mixed collection of data. The C++ class not only holds data like a structure, but also adds a set of functions for manipulating the data and access protection.

Turning the struct stack into a class you get:

 class stack {     private:         int count;              // Number of items in the stack         int data[STACK_SIZE];   // The items themselves     public:         // Initialize the stack         void init(  );         // Push an item on the stack         void push(const int item);         // Pop an item from the stack         int pop(  ); }; 

Let's go into this class declaration in more detail. The beginning looks much like a structure definition, except that you're using the word class instead of struct :

 class stack {     private:         int count;              // Number of items in the stack         int data[STACK_SIZE];   // The items themselves 

This declares two fields: count and data . In a class these items are not called fields; they are called member variables . The keyword private indicates the access privileges associated with these two member variables.

There are three levels of access privileges: public , private , and protected . Class members , both data and functions, marked private cannot be used outside the class. They can be accessed only by functions within the class. The opposite of private is public , which indicates members that anyone can access.

Finally, protected is similar to private except that it allows access by derived classes. (We discuss derived classes in Chapter 21.)

You've finished defining the data for this class. Now you need to define the functions that manipulate the data:

 public:         // Initialize the stack         void init(  );         // Push an item on the stack         void push(const int item);         // Pop an item from the stack         int pop(  ); }; 

This section starts with the keyword public . This tells C++ that you want all these member functions to be available to the outside. In this case, you just define the function prototypes . The code for the functions will be defined later.

Next comes the body of the init function. Since this function belongs to the stack class, you prefix the name of the procedure with stack:: . (We discuss the scope operator :: in more detail in Chapter 14.)

The definition of the init function looks like this:

 inline void stack::init(  ) {     count = 0;  // Zero the stack } 

This procedure zeroes the stack's count . In the structure version of the stack_init function, you must pass the stack in as a parameter. Since this function is part of the stack class, that's unnecessary. This also means that you can access the member variables directly. In other words, instead of having to say the_stack.count , you just say count .

The functions push and pop are implemented in a similar manner:

 inline void stack::push(const int item) {     data[count] = item;     ++count; } inline int stack::pop(  ) {     // Stack goes down by one     --count;     // Then we return the top value     return (data[count]); } 

The stack class is now complete. All you have to do is use it.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net