#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)类型转换

本文详细解析了C++中构造函数与析构函数的特点,包括构造函数的参数区分、无返回值特性及构造函数的三个主要作用:构造对象、初始化对象和类型转换。通过实例代码演示了构造函数的实现方式,并展示了其在VC++6.0下的输出结果。

被折叠的 条评论
为什么被折叠?



