经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
Open closed principle
来源:cnblogs  作者:A_Elite  时间:2018/10/19 9:19:57  对本文有异议
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. class Book
  5. {
  6. public:
  7. string getContents()
  8. {
  9. return "Long time ago,There is a temple in the mountain";
  10. }
  11. };
  12. class Paper
  13. {
  14. public:
  15. string getContents()
  16. {
  17. return "I am handsome";
  18. }
  19. };
  20. class Mother
  21. {
  22. public:
  23. void tellstory(Book* b)
  24. {
  25. cout << b->getContents() << endl;
  26. }
  27. void tellstory(Paper* p)
  28. {
  29. cout << p->getContents() << endl;
  30. }
  31. };
  32. int main(int argc, char *argv[])
  33. {
  34. Book b;
  35. Mother m;
  36. Paper p;
  37. m.tellstory(&b);
  38. m.tellstory(&p);
  39. return 0;
  40. }



 

 

  1. #include <iostream>
  2.  
  3. using namespace std;
  4. class iReader
  5. {
  6. public:
  7. virtual string getContents() = 0;//only provide a interface
  8. };
  9. class Book : public iReader
  10. {
  11. public:
  12. string getContents()
  13. {
  14. return "Long time ago,There is a temple in the mountain";
  15. }
  16. };
  17.  
  18. class Paper : public iReader
  19. {
  20. public:
  21. string getContents()
  22. {
  23. return "I am handsome";
  24. }
  25. };
  26.  
  27. class Mother
  28. {
  29. public:
  30. void tellstory(iReader * r)
  31. {
  32. cout << r->getContents() << endl;
  33. }
  34. };
  35.  
  36. int main(int argc, char *argv[])
  37. {
  38. Book b;
  39. Mother m;
  40. Paper p;
  41. m.tellstory(&b);
  42. m.tellstory(&p);
  43. return 0;
  44. }
  1.  

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号