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

  1. //: C15:Early.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. // Early binding & virtuals
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. class Base {
  11. public:
  12.   virtual int f() const { return 1; }
  13. };
  14.  
  15. class Derived : public Base {
  16. public:
  17.   int f() const { return 2; }
  18. };
  19.  
  20. int main() {
  21.   Derived d;
  22.   Base* b1 = &d;
  23.   Base& b2 = d;
  24.   Base b3;
  25.   // Late binding for both:
  26.   cout << "b1->f() = " << b1->f() << endl;
  27.   cout << "b2.f() = " << b2.f() << endl;
  28.   // Early binding (probably):
  29.   cout << "b3.f() = " << b3.f() << endl;
  30. } ///:~
  31.