求助C++一题

来源:百度知道 编辑:UC知道 时间:2024/06/20 17:06:59
23.下面程序的运行结果是________。
#include "iostream.h"
class test
{
private:
int num;
float fl;
public:
test( );
int getint( ){return num;}
float getfloat( ){return fl;}
~test( );
};

test::test( )
{
cout << "Initalizing default" << endl;
num=0;fl=0.0;
}

test::~test( )
{
cout << "Desdtructor is active" << endl;
}

void main( )
{
test array[2];
cout << array[1].getint( )<< " " << array[1].getfloat( ) <<endl;
}
希望有点过程,谢谢。。。

Initalizing default
Initalizing default
0 0
Desdtructor is active
Desdtructor is active
执行到test array[2];时,因为是两个,所以连续调用两次构构造函数test()
然后就是你所写出的cout << array[1].getint( )<< " " << array[1].getfloat( ) <<endl输出;

这时main()函数快要结束.所以你定义的两个test对象要被撒毁,所以调用了你定义的析构函数~test()

Initalizing default
Initalizing default
0 0
Desdtructor is active
Desdtructor is active
Press any key to continue