收藏 分享(赏)

C++中对象的常引用.docx

上传人:gnk289057 文档编号:7217858 上传时间:2019-05-10 格式:DOCX 页数:4 大小:52.93KB
下载 相关 举报
C++中对象的常引用.docx_第1页
第1页 / 共4页
C++中对象的常引用.docx_第2页
第2页 / 共4页
C++中对象的常引用.docx_第3页
第3页 / 共4页
C++中对象的常引用.docx_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

1、【C+ 中对象的常引用】直接传递对象名用对象名做函数参数时,在函数调用时将建立一个新的对象,它是形参对象的拷贝。=下面给出一个直接传递对象名的例子程序 1.1=1. #include 2. using namespace std; 3. class Time 4. 5. public: 6. Time(int,int,int);/构造函数 7. void Print();/输出信息函数 8. void reset(Time t);/重置函数 9. private: 10. int year; 11. int month; 12. int day; 13.; 14.Time:Time(int y

2、,int m,int d) 15. 16. year=y; 17. month=m; 18. day=d; 19. 20.void Time:Print() 21. 22. cout 2. using namespace std; 3. class Time 4. 5. public: 6. Time(int,int,int);/构造函数 7. void Print();/输出信息函数 8. void reset(Time /重置函数 =对这一行代码进行了修改= 9. private: 10. int year; 11. int month; 12. int day; 13. ; 14. Ti

3、me:Time(int y,int m,int d) 15. 16. year=y; 17. month=m; 18. day=d; 19. 20. void Time:Print() 21. 22. cout 2. using namespace std; 3. class Time 4. 5. public: 6. Time(int,int,int);/构造函数 7. void Print();/输出信息函数 8. void reset(const Time /重置函数 9. private: 10. int year; 11. int month; 12. int day; 13. ;

4、14. Time:Time(int y,int m,int d) 15. 16. year=y; 17. month=m; 18. day=d; 19. 20. void Time:Print() 21. 22. coutyear“/“month“/“dayendl; 23. 24. void Time:reset(const Time 27. t.month=0; 28. t.day=0; 29. 30. int main() 31. 32. Time t1(12,12,12); 33. t1.Print(); 34. t1.reset(t1); 35. t1.Print(); 36. return 0; 37. 该程序会报错,因为在函数 reset 中,企图修改对象 t 中的数据成员的值

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报