home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C15 / Wind5.cpp < prev   
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.8 KB  |  86 lines

  1. //: C15:Wind5.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. // Pure abstract base classes
  7. #include <iostream>
  8. using namespace std;
  9. enum note { middleC, Csharp, Cflat }; // Etc.
  10.  
  11. class Instrument {
  12. public:
  13.   // Pure virtual functions:
  14.   virtual void play(note) const = 0;
  15.   virtual char* what() const = 0;
  16.   // Assume this will modify the object:
  17.   virtual void adjust(int) = 0;
  18. };
  19. // Rest of the file is the same ...
  20.  
  21. class Wind : public Instrument {
  22. public:
  23.   void play(note) const {
  24.     cout << "Wind::play" << endl;
  25.   }
  26.   char* what() const { return "Wind"; }
  27.   void adjust(int) {}
  28. };
  29.  
  30. class Percussion : public Instrument {
  31. public:
  32.   void play(note) const {
  33.     cout << "Percussion::play" << endl;
  34.   }
  35.   char* what() const { return "Percussion"; }
  36.   void adjust(int) {}
  37. };
  38.  
  39. class Stringed : public Instrument {
  40. public:
  41.   void play(note) const {
  42.     cout << "Stringed::play" << endl;
  43.   }
  44.   char* what() const { return "Stringed"; }
  45.   void adjust(int) {}
  46. };
  47.  
  48. class Brass : public Wind {
  49. public:
  50.   void play(note) const {
  51.     cout << "Brass::play" << endl;
  52.   }
  53.   char* what() const { return "Brass"; }
  54. };
  55.  
  56. class Woodwind : public Wind {
  57. public:
  58.   void play(note) const {
  59.     cout << "Woodwind::play" << endl;
  60.   }
  61.   char* what() const { return "Woodwind"; }
  62. };
  63.  
  64. // Identical function from before:
  65. void tune(Instrument& i) {
  66.   // ...
  67.   i.play(middleC);
  68. }
  69.  
  70. // New function:
  71. void f(Instrument& i) { i.adjust(1); }
  72.  
  73. int main() {
  74.   Wind flute;
  75.   Percussion drum;
  76.   Stringed violin;
  77.   Brass flugelhorn;
  78.   Woodwind recorder;
  79.   tune(flute);
  80.   tune(drum);
  81.   tune(violin);
  82.   tune(flugelhorn);
  83.   tune(recorder);
  84.   f(flugelhorn);
  85. } ///:~
  86.