C++题目求解

来源:百度知道 编辑:UC知道 时间:2024/05/30 23:23:54
#include "iostream.h"
#include "stdlib.h"
class Sample
{
public:
int x,y;
Sample() {x=y=0;}
Sample(int a, int b)
{x=a;y=b;}
void disp()
{
cout<<"x="<<x<<",y="<<y<<endl;
}
~Sample()
{
if(x==y)
cout<<"x=y"<<endl;
else
cout<<"x!=y"<<endl;
}
};

void main()
{
Sample s1(2,3);
s1.disp();
if(s1.x==2)
exit(0);
}

运行后x= 2, y =3
为什么没有x = y

直接EXIT了,没有析够

干嘛要用exit(0)
exit()可能没有调用析构函数
用:
if(s1.x==2) ;
return 0;

exit()就是清理程序了。
程序不正常退出,使用exit();

用 return 0;