求助亚
能不能帮我分析下这个是怎么运行的
#include<iostream.h>
class B
{
int x,y;
public:
B() {x=y=0;cout<<"constructor1"<<endl;}
B(int i) {x=i;y=0;cout<<"constructor2"<<endl;}
B(int i,int j){x=i;y=j;cout<<"constructor3"<<endl;}
~B() {cout<<"Destructor"<<endl;}
void print()
{
cout<<"x="<<x<<",y="<<y<<endl;
}
};
void main()
{
B *p;
p=new B[3];
p[0]=B();
P[1]=B(5);
P[2]=B(2,3);
for (int i=0;i<3;i++)
p[i].print();
delete[] p;
}
运行结果是
constructor1
constructor1
constructor1
constructor1
destructor
constructor2
destructor
constructor3
destructor
x=0,y=0
x=5,y=0
x=2,y=3
destructor
destructor
destructor