您的位置:首页 > 编程语言 > C语言/C++

C++问题小结--3.构造函数与析构函数关于其自身特点的简单代码陈述

2016-04-18 22:09 896 查看
#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)类型转换
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: