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

  1. //: C14:Wind.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. // Inheritance & upcasting
  7. enum note { middleC, Csharp, Cflat }; // Etc.
  8.  
  9. class Instrument {
  10. public:
  11.   void play(note) const {}
  12. };
  13.  
  14. // Wind objects are Instruments
  15. // because they have the same interface:
  16. class Wind : public Instrument {};
  17.  
  18. void tune(Instrument& i) {
  19.   // ...
  20.   i.play(middleC);
  21. }
  22.  
  23. int main() {
  24.   Wind flute;
  25.   tune(flute); // Upcasting
  26. } ///:~
  27.