『独習 C++ 第4判』という参考書の練習問題に
『ストップウォッチをまねた機能を持つクラスを作成しなさい。』
という課題があったので解いてみました。
► スポイラーを表示
// 44p 問__2
#include
#include
#include
using namespace std;
#define ON__ true
#define OFF_ false
class stopwatch {
clock_t m_Timer;
clock_t m_top;
clock_t m_end;
bool m_Switch;
public:
stopwatch(); // コンストラクタ
~stopwatch(); // デストラクタ
void start();
void stop();
void show();
};
// 初期化処理
stopwatch::stopwatch() {
m_Timer = 0;
m_Switch = ON__;
m_top = clock();
}
// 破棄処理
stopwatch::~stopwatch() {
if (m_Switch == ON__) {
m_end = clock();
m_Timer += m_end - m_top;
}
cout タイマーオフ\n"
タイマーオン\n"
途中結果の確認\n"
終了\n";
while (cin >> Choice, 0 != strcmp(Choice, "End")) {
if (0 == strcmp(Choice, "Start")) {
ob.start();
}
if (0 == strcmp(Choice, "Stop")) {
ob.stop();
}
if (0 == strcmp(Choice, "Show")) {
ob.show();
}
}
// main()ローカル オブジェクト ob が、スコープが外されて消失する... その際に
// デストラクタ関数が実行される
return 0;
}