Command Line Arguments

Chapter 19 - Working in an Object-oriented Environment

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

An Object-oriented Stack
Chapter 16 introduced many object-oriented concepts. For example, the C++ class was introduced as an abstract data type that provides the encapsulation of data structures and the operations on those structures (member functions). In this capacity, the C++ class serves as the mechanism for forming objects. The following simple example of object creation with a C++ class demonstrates the implementation of an object-oriented stack.
The traditional FILO (first in, last out) manner is used for the stack operations in this example. The stack class provides six member functions or methods: clear( ), top( ), empty( ), full( ), push( ), and pop( ). Examine the following listing and observe how these member functions are implemented:
//
//  stack.cpp
//  C++ program illustrates object-oriented programming
//  with a classical stack operation using a string of
//  characters.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

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

#define maxlen 80

class stack {
 char str1[maxlen];
 int  first;
public:
 void clear(void);
 char top(void);
 int  empty(void);
 int  full(void);
 void push(char chr);
 char pop(void);
};

void stack::clear(void)
{
 first=0;
}
char stack::top(void)
{
 return (str1[first]);
}

int stack::empty(void)
{
 return (first==0);
}

int stack::full(void)
{
 return (first==maxlen-1);
}

void stack::push(char chr)
{
 str1[++first]=chr;
}
char stack::pop(void)
{
 return (str1[first—]);
}

main( )
{
 stack mystack;
 char str[11]="0123456789";

 // clear the stack
 mystack.clear( );
 // load the string, char-by-char, on the stack
 cout << “\nLoad character data on stack” << endl;
 for(int i=0; (int) i<strlen(str);i++) {
   if (!mystack.full( ))
     mystack.push(str[i]);
     cout << str[i] << endl;
 }

 // unload the stack, char-by-char
 cout << “\nUnload character data from stack” << endl;
 while (!mystack.empty( ))
   cout << mystack.pop( ) << endl;
 
 return (0);
}
In this application, characters from a string are pushed, one character at a time, onto the stack. Then the stack is unloaded one character at a time. Loading and unloading are done from the stack top, so the first character information loaded on the stack is pushed down most deeply in the stack.
Notice in the following listing that the character for the number zero was pushed onto the stack first. It should be no surprise that it is the last character popped off the stack.
Load character data on stack
0
1
2
3
4
5
6
7
8
9

Unload character data from stack
9
8
7
6
5
4
3
2
1
0
This example lacks many of the more advanced object-oriented concepts such as memory management, inheritance, and polymorphism. However, the example is a complete object-oriented program. The power of object-oriented thinking becomes more apparent as more and more of Meyer’s seven points are actually implemented.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

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