⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wind5.cpp

📁 希望我提供的代码对大家有帮助
💻 CPP
字号:
//: C15:Wind5.cpp

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

// Pure abstract base classes

#include <iostream>

using namespace std;

enum note { middleC, Csharp, Cflat }; // Etc.



class Instrument {

public:

  // Pure virtual functions:

  virtual void play(note) const = 0;

  virtual char* what() const = 0;

  // Assume this will modify the object:

  virtual void adjust(int) = 0;

};

// Rest of the file is the same ...



class Wind : public Instrument {

public:

  void play(note) const {

    cout << "Wind::play" << endl;

  }

  char* what() const { return "Wind"; }

  void adjust(int) {}

};



class Percussion : public Instrument {

public:

  void play(note) const {

    cout << "Percussion::play" << endl;

  }

  char* what() const { return "Percussion"; }

  void adjust(int) {}

};



class Stringed : public Instrument {

public:

  void play(note) const {

    cout << "Stringed::play" << endl;

  }

  char* what() const { return "Stringed"; }

  void adjust(int) {}

};



class Brass : public Wind {

public:

  void play(note) const {

    cout << "Brass::play" << endl;

  }

  char* what() const { return "Brass"; }

};



class Woodwind : public Wind {

public:

  void play(note) const {

    cout << "Woodwind::play" << endl;

  }

  char* what() const { return "Woodwind"; }

};



// Identical function from before:

void tune(Instrument& i) {

  // ...

  i.play(middleC);

}



// New function:

void f(Instrument& i) { i.adjust(1); }



int main() {

  Wind flute;

  Percussion drum;

  Stringed violin;

  Brass flugelhorn;

  Woodwind recorder;

  tune(flute);

  tune(drum);

  tune(violin);

  tune(flugelhorn);

  tune(recorder);

  f(flugelhorn);

} ///:~

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -