5_2.cpp
来自「清华大学C++补充习题代码」· C++ 代码 · 共 35 行
CPP
35 行
//5_2.cpp
#include <iostream>
using namespace std;
class Width
{
public:
Width() { ++count; }
~Width() { --count; }
int numWidths() { return count; }
private:
static int count;
};
int Width::count = 0;
int main()
{ Width w, x;
cout << "现在是: " << w.numWidths() << " Widths.\n";
{ Width w, x, y, z;
cout << "现在是: " << w.numWidths() << " Widths.\n";
}
cout << "现在是: " << w.numWidths() << " Widths.\n";
Width y;
cout << "现在是: " << w.numWidths() << " Widths.\n";
return 0;
}
/*
程序运行结果为:
现在是: 2 Widths.
现在是: 6 Widths.
现在是: 2 Widths.
现在是: 3 Widths.
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?