您的位置:首页 > 其它

第六周阅读程序4:对象的复制

2015-04-15 09:20 211 查看
问题及代码:

#include <iostream>
using namespace std;
class example
{
public:
example()
{
cout<<"Default Constructing! "<<endl;
}
example(int n)
{
i=n;
cout<<"Constructing: "<<i<<endl;
}
~example()
{
cout <<"Destructing: "<<i<<endl;
}
int get_i()
{
return i;
}
private:
int i;
};
int sqr_it(example o)
{
return o.get_i()* o.get_i();
}
int main()
{
example x(10);             //执行一次带默认参数的构造函数,i=10  输出  Constructing: 10
cout<<x.get_i()<<endl;     //输出  10
cout<<sqr_it(x)<<endl;     //在x.get_i()函数中,新建一个example o 执行一次复制构造函数,将x的数据复制给o,然后输出  100
//函数调用结束,进行一次o的析构函数,输出   Destructing:10
return 0;                  //程序结束,执行x的析构函数输出 Destructing: 10
}


运行结果:



知识点总结:

对象的复制,执行复制构造函数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: