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

  1. //: C15:Addv.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. // Adding virtuals in derivation
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. class Base {
  11.   int i;
  12. public:
  13.   Base(int ii) : i(ii) {}
  14.   virtual int value() const { return i; }
  15. };
  16.  
  17. class Derived : public Base {
  18. public:
  19.   Derived(int ii) : Base(ii) {}
  20.   int value() const {
  21.     return Base::value() * 2;
  22.   }
  23.   // New virtual function in the Derived class:
  24.   virtual int shift(int x) const {
  25.     return Base::value() << x;
  26.   }
  27. };
  28.  
  29. int main() {
  30.   Base* B[] = { new Base(7), new Derived(7) };
  31.   cout << "B[0]->value() = "
  32.        << B[0]->value() << endl;
  33.   cout << "B[1]->value() = "
  34.        << B[1]->value() << endl;
  35. //! cout << "B[1]->shift(3) = "
  36. //!      << B[1]->shift(3) << endl; // Illegal
  37. } ///:~
  38.