- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第5章 面向对象程序设计风格 void Fibonacci:: gen_elems(int pos) const { if (_elems.empty()) { _elems.push_back(1); _elems.push_back(1); } if (_elems.size() pos) { int ix = _elems.size(); int n_2 = _elems[ix-2]; int n_1 = _elems[ix-1]; for (; ix pos; ++ix) { int elem = n_2 + n_1; _elems.push_back(elem); n_2 = n_1; n_1 = elem; } } } 第5章 面向对象程序设计风格 ostream Fibonacci :: print(ostream os) const { int elem_pos = _beg_pos-1; int end_pos = elem_pos + _length; if (end_pos _elems.size()) Fibonacci::gen_elems(end_pos); while (elem_pos end_pos) os _elems[elem_pos++] ; return os; } 第5章 面向对象程序设计风格 考虑到模块的完整性: class Fibonacci : public num_sequence { public: // ... protected: bool check_integrity(int pos) const; // ... }; 第5章 面向对象程序设计风格 考虑到模块的完整性: class Fibonacci : public num_sequence { public: // ... protected: bool check_integrity(int pos) const; // ... }; 第5章 面向对象程序设计风格 同名屏蔽: int Fibonacci:: elem(int pos) const { // now resolves to Fibonaccis instance if (! check_integrity(pos)) return 0; // ... } 第5章 面向对象程序设计风格 作用域操作符: inline bool Fibonacci:: check_integrity(int pos) const { // class scope operator necessary ... // unqualified name resolves to this instance! if (! num_sequence::check_integrity(pos)) return false; if (pos _elems.size()) Fibonacci::gen_elems(pos); return true; } 第5章 面向对象程序设计风格 针对基类编程所带来的问题: void Fibonacci::example() { num_sequence *ps = new Fibonacci(12, 8); // ok: resolves to Fibonacci::elem() through virtual mechanism ps-elem(1024); // oops: resolves statically to //num_sequence::check_integrity() // based on the type of ps ps-check_integrity(pos); } 第5章 面向对象程序设计风格 解决问题的方法: bool num_sequence:: check_integrity(int pos, int size) { if (pos = 0 || pos max_seq){ // same as before ... } if (pos size) // gen_elems() is invoked through virtual mechanism gen_elems(pos); return true; } 第5章 面向对象程序设计风格 简化以后的 elem(): int Fibonacci:: elem(int pos) { if (! check_integrity(pos, _elems.size())) r
您可能关注的文档
最近下载
- 西南18J112 墙标准图集.pdf VIP
- 2025-2026学年高一上学期《树立正确三观:从庞众望的成长看青春担当》主题班会课件.pptx
- 北京市海淀区2024~2025学年七年级上学期期中考试数学试卷.docx
- 2025电力数据资产管理体系白皮书.docx VIP
- 《运动神经元病》课件.pptx VIP
- 肾上腺皮质腺瘤护理查房.pptx VIP
- 药物制剂生产实训(初级)课件 2-2 PPT:人员卫生管理.pptx
- 三年(2023-2025)中考历史真题分类汇编:专题07 统一多民族国家的巩固与发展·选择题(全国通用)(解析版).docx VIP
- 环境工程原理课件.pptx VIP
- 5_1_名雅化工不饱和聚脂树脂腻子(原子灰)MSDS.docx VIP
文档评论(0)