选择题
下列程序的执行结果是______。
#include<iostream.h>
#include<stdlib.h>
class TestClass
{
public:
int x,y;
TestClass () {x=y=0;}
TestClass (int a,int b){x=a;y=b;}
void disp()
{
cout<<'x='<<x<<',y='<<y<<end1;
}
void main()
TestClass s1(2,3);
s1.disp();
}
【正确答案】
B
【答案解析】由主函数入手,定义了类TestClass的对象s1(2,3),当类对象进入其作用域时调用构造函数,构造函数应读是调用具有参数的“TestClass(int a,int b){x=a;y=b;)”。然后引用成员函数disp(),则输出为“cout<<”x=“<<x<<”,y=“<<y<<end1;”。即x=2,y=3。