C++ 程序,错在哪里 ?

来源:百度知道 编辑:UC知道 时间:2024/09/23 13:28:34
class alpha
{
private:
static int count; //静态数据
public:
alpha() //构造函数
{
count++;
}
static void display_count() //静态成员函数
{
cout << count;
cout << endl;
}
};
int alpha::count = 0;
int main()
{
//在创建任何对象之前
alpha::display_count();

alpha obj1, obj2, obj3;

//在创建了三个对象之后
alpha::display_count();

return 0;
}

貌似 int alpha::count = 0; 中的等号有点问题,下面是正确的

#include <iostream>
using namespace std;
class alpha
{
private:
static int count; //静态数据
public:
alpha() //构造函数
{
count++;
}
static void display_count() //静态成员函数
{
cout << count;
cout << endl;
}
};
int alpha::count =0;
int main()
{
//在创建任何对象之前
alpha::display_count();

alpha obj1, obj2, obj3;

//在创建了三个对象之后
alpha::display_count();

return 0;
}