friend2.cpp

来自「Thinking_in_C___2.rar」· C++ 代码 · 共 45 行

CPP
45
字号
//: C03:Friend2.cpp
// From Thinking in C++, 2nd Edition
// at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// Making an entire class a friend
class Watch {
  int time;  // A measure of time
  int alarm;  // When the alarm goes off
  int date;   // Other things a Watch should know
public:
  // Constructor sets starting state:
  Watch() { time = alarm = date = 0; }
  void tick() { time++; } // Very simple transition
  // Allow all members of MicrowaveOven access to private
  // Elements of Watch:
  friend class MicrowaveOven;
};

class MicrowaveOven {
  int time;
  int start_time;
  int stop_time;
  int intensity;
public:
  MicrowaveOven() {
    time = 0;
    start_time = stop_time = 0;
    intensity = 0;
  }
  void tick() { time++; }
  void synchronize(Watch & WA) {
    time = WA.time = 15;  // Set both to a common state
  }
};

int main() {
  Watch que_hora;
  MicrowaveOven nuke;
  que_hora.tick();
  que_hora.tick();
  nuke.tick();
  nuke.synchronize(que_hora);
} ///:~

⌨️ 快捷键说明

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