home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / 3-4.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  1KB  |  51 lines

  1. /* Copyright (c) 1992 by AT&T Bell Laboratories. */
  2. /* Advanced C++ Programming Styles and Idioms */
  3. /* James O. Coplien */
  4. /* All rights reserved. */
  5.  
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. class ParseTree {
  10.     // . . . .
  11. };
  12.  
  13. class Node {
  14.     // . . . .
  15. public:
  16.     Node *leftChild() const, * rightChild() const;
  17.     const char * const contents() const;
  18. };
  19.  
  20. class String {
  21. public:
  22.     String operator+(const String &s) const;
  23.     String();
  24.     String(const String& s);
  25.     String(const char *s);
  26.     String operator=(const String& q);
  27.     ~String();
  28.     operator ParseTree() { /* . . . . */ }
  29.     String(Node);
  30.     int length();
  31. private:
  32.     String nodeWalk(const Node*);
  33.     char *p;
  34. };
  35.  
  36.  
  37. String
  38. String::nodeWalk(const Node *n) {      // infix node walk
  39.     if (n == 0) return "";
  40.     String retval = String("(") + nodeWalk(n->leftChild());
  41.     retval = retval + " " + n->contents();
  42.     retval = retval + " " + nodeWalk(n->rightChild()) + ")";
  43. }
  44.  
  45. String::String(Node n)
  46. {
  47.     String temp = nodeWalk(&n);
  48.     p = new char[temp.length() + 1];
  49.     ::strcpy(p, temp.p);
  50. }
  51.