home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C22 / Paste.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.2 KB  |  54 lines

  1. //: C22:Paste.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} Vendor
  7. // Fixing a mess with MI
  8. #include "Vendor.h"
  9. #include <fstream>
  10. using namespace std;
  11.  
  12. ofstream out("paste.out");
  13.  
  14. class MyBase { // Repair Vendor interface
  15. public:
  16.   virtual void v() const = 0;
  17.   virtual void f() const = 0;
  18.   // New interface function:
  19.   virtual void g() const = 0;
  20.   virtual ~MyBase() { out << "~MyBase()\n"; }
  21. };
  22.  
  23. class Paste1 : public MyBase, public Vendor1 {
  24. public:
  25.   void v() const {
  26.     out << "Paste1::v()\n";
  27.     Vendor1::v();
  28.   }
  29.   void f() const {
  30.     out << "Paste1::f()\n";
  31.     Vendor1::f();
  32.   }
  33.   void g() const {
  34.     out << "Paste1::g()\n";
  35.   }
  36.   ~Paste1() { out << "~Paste1()\n"; }
  37. };
  38.  
  39. int main() {
  40.   Paste1& p1p = *new Paste1;
  41.   MyBase& mp = p1p; // Upcast
  42.   out << "calling f()\n";
  43.   mp.f();  // Right behavior
  44.   out << "calling g()\n";
  45.   mp.g(); // New behavior
  46.   out << "calling A(p1p)\n";
  47.   A(p1p); // Same old behavior
  48.   out << "calling B(p1p)\n";
  49.   B(p1p);  // Same old behavior
  50.   out << "delete mp\n";
  51.   // Deleting a reference to a heap object:
  52.   delete ∓ // Right behavior
  53. } ///:~
  54.