急解一个C++程序的输出结果~!!!!

来源:百度知道 编辑:UC知道 时间:2024/09/22 21:16:11
#include<iostream.h>
using namespace std;
class B
{
public:
B(int a=0,int b=0)
{
x=a;y=b; countB++;
cout<<"Object id is"<<countB<<":"<<x<<" "<<y<<endl;
}
~B(){countB--;}
private:
int x,y; static int coountB;
};
int B::countB=0;
int main()
{
B b1;
B b2(1,1);
}

静态变量要先初始化,并赋值,所以countB是1,而不是0!
Object id is1:0 0
Object id is2:1 1

Object id is 0:0 0
Object id is 0:1 1

结果为:Object id is1:0 0
Object id is2:1 1

另外上面的有些小错误:
#include<iostream>
using namespace std;
class B
{
public:
B(int a=0,int b=0)
{
x=a;y=b; countB++;
cout<<"Object id is"<<countB<<":"<<x<<" "<<y<<endl;
}
~B(){countB--;}
private:
int x,y; static int countB;
};
int B::countB=0;
void main()
{
B b1;
B b2(1,1);
}