home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C14 / InheritStack2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  916 b   |  40 lines

  1. //: C14:InheritStack2.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. //{L} ../C13/Stack4
  7. // Composition vs. inheritance
  8. #include "../C13/Stack4.h"
  9. #include "../require.h"
  10. #include <iostream>
  11. #include <fstream>
  12. #include <string>
  13. using namespace std;
  14.  
  15. class StringList {
  16.   Stack stack; // Embed instead of inherit
  17. public:
  18.   void push(string* str) {
  19.     stack.push(str);
  20.   }
  21.   string* peek() const {
  22.     return (string*)stack.peek();
  23.   }
  24.   string* pop() {
  25.     return (string*)stack.pop();
  26.   }
  27. };
  28.  
  29. int main() {
  30.   ifstream file("InheritStack2.cpp");
  31.   assure(file, "InheritStack2.cpp");
  32.   string line;
  33.   StringList textlines;
  34.   while(getline(file,line))
  35.     textlines.push(new string(line));
  36.   string* s;
  37.   while((s = textlines.pop()) != 0) // No cast!
  38.     cout << *s << endl;
  39. } ///:~
  40.