| I l@ve RuBoard |
|
13.3 Using a ClassUsing a class is much like using a structure. Declaring a class variable is the same, except you use the word class instead of struct: class stack a_stack; // Stack we want to use The word class is not needed and is frequently omitted: stack a_stack; // Stack we want to use You access the members of a structure using a dot; for example: structure.field = 5; Accessing the members of a class is similar, except that the members of a class can be both data and functions. Also, you can access only the members that are public. To call the init member function of the stack class, all you need to do is this: a_stack.init( ); The push and pop member functions can be accessed in a similar manner: a_stack.push(1);
result = a_stack.pop( );
Example 13-2 contains a class version of the stack. Example 13-2. stack_c/stack_c.cpp/********************************************************
* Stack *
* A file implementing a simple stack class *
********************************************************/
#include <cstdlib>
#include <iostream>
#include <assert.h>
const int STACK_SIZE = 100; // Maximum size of a stack
/********************************************************
* Stack class *
* *
* Member functions *
* init -- initialize the stack. *
* push -- put an item on the stack. *
* pop -- remove an item from the stack. *
********************************************************/
// The stack itself
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( );
};
/********************************************************
* stack::init -- initialize the stack. *
********************************************************/
inline void stack::init( )
{
count = 0; // Zero the stack
}
/********************************************************
* stack::push -- push an item on the stack. *
* *
* Warning: We do not check for overflow. *
* *
* Parameters *
* item -- item to put in the stack *
********************************************************/
inline void stack::push(const int item)
{
assert((count >= 0) &&
(count < sizeof(data)/sizeof(data[0])));
data[count] = item;
++count;
}
/********************************************************
* stack::pop -- get an item off the stack. *
* *
* Warning: We do not check for stack underflow. *
* *
* Returns *
* The top item from the stack. *
********************************************************/
inline int stack::pop( )
{
// Stack goes down by one
--count;
assert((count >= 0) &&
(count < sizeof(data)/sizeof(data[0])));
// Then we return the top value
return (data[count]);
}
// A short routine to test the stack
int main( )
{
stack a_stack; // Stack we want to use
a_stack.init( );
// Push three value on the stack
a_stack.push(1);
a_stack.push(2);
a_stack.push(3);
// Pop the item from the stack
std::cout << "Expect a 3 ->" << a_stack.pop( ) << '\n';
std::cout << "Expect a 2 ->" << a_stack.pop( ) << '\n';
std::cout << "Expect a 1 ->" << a_stack.pop( ) << '\n';
return (0);
}
|
| I l@ve RuBoard |
|