第六周阅读程序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 }
运行结果:
知识点总结:
对象的复制,执行复制构造函数
相关文章推荐
- 第六周 程序阅读-深复制、友元、类模板(4)
- 第六周 程序阅读-深复制、友元、类模板(3)
- 第六周阅读程序3:复制构造函数
- 第六周 阅读程序 3 类对象成员的赋值
- 第六周 程序阅读-深复制、友元、类模板
- 第六周 程序阅读-深复制、友元、类模板(5)
- 第六周项目零:阅读程序(5):结构函数和析构函数,对象的赋值
- 第六周 阅读程序 5 建立对象的赋值函数 动态对象的运用
- 第六周 阅读程序 (3)
- visio 对象复制到其他程序
- 第14周 《C++语言基础》程序阅读——标准输入输出对象及文本文件 (1)
- 第14周 《C++语言基础》程序阅读——标准输入输出对象及文本文件 (3)(2)
- 第五周阅读程序2:数组操作对象
- 第六周阅读程序—b
- 第六周项目零:阅读程序(3):结构函数和析构函数
- 第六周阅读程序
- 第十四周阅读程序2:运用文件流进行文件的复制
- 第一周项目 程序阅读——初识对象 1.1
- 第六周阅读程序 (3)
- 第六周 程序阅读