#include<iostream>
using namespace std;
class Test
{
public:
Test()
{
cout<<"Create Test Object:"<<this<<endl;
}
Test(int)
{
cout<<"Create Test1 Object:"<<this<<endl;
}
Test(int,int)
{
cout<<"Create Test2 Object:"<<this<<endl;
}
~Test()
{
cout<<"Free Test Object:"<<this<<endl;
}
private:
int data;
};
int main()
{
Test t;
Test t1(10);
Test t2(20,30);
}
在VC++6.0下的输出结果见下图:
通过上述代码我们易见构造函数与析构函数的特点:
(1)构造函数由不同的参数表区分
(2)构造函数与析构函数都无返回值
构造函数的三个作用:
(1)构造对象
(2)初始化对象
(3)类型转换