#include using namespace std; class Box { public: // 成员为静态时,这意味着无论创建多少个类的对象,静态成员都只有一个副本 // 静态成员在类的所有对象中是共享的。 // 如果不存在其他的初始化语句,在创建第一个对象时,所有的静态数据都会被初始化为零。 // 不能把静态成员放置在类的定义中 // static int objectCount = 0; static int objectCount; // 构造函数定义 Box(double l, double b, double h) { cout <<"Constructor called." << endl; length = l; breadth = b; height = h; // 每次创建对象时增加 1 objectCount++; } double Volume() { return length * breadth * height; } private: double length; // 长度 double breadth; // 宽度 double height; // 高度 }; // 初始化类 Box 的静态成员 int Box::objectCount = 0; int main(void) { Box Box1(3.3, 1.2, 1.5); // 声明 box1 Box Box2(8.5, 6.0, 2.0); // 声明 box2 // 输出对象的总数 cout << "Total objects: " << Box::objectCount << endl; return 0; }