关键词搜索

源码搜索 ×
×

关于多态(polymorphism)的初步知识(此处指动态多态)

发布2013-05-15浏览7765次

详情内容

  1. #include <iostream>
  2. using namespace std;
  3. class Human
  4. {
  5. public:
  6. virtual void eat()
  7. {
  8. cout << "Human: virtual void eat()" << endl;
  9. }
  10. void sleep() // 非虚函数,不具有多态性
  11. {
  12. cout << "Human: void sleep()" << endl;
  13. }
  14. };
  15. class Man : public Human
  16. {
  17. public:
  18. virtual void eat() // 此处的virtual可省略,但最好不要省略
  19. {
  20. cout << "Man: virtual void eat()" << endl;
  21. }
  22. void sleep()
  23. {
  24. cout << "Man: void sleep()" << endl;
  25. }
  26. };
  27. int main()
  28. {
  29. Human *p, h;
  30. Man m;
  31. p = &h;
  32. p->eat();
  33. p->sleep();
  34. p = &m;
  35. p->eat();
  36. p->sleep();
  37. return 0;
  38. }

      结果为:

Human: virtual void eat()
Human: void sleep()
Man: virtual void eat()
Human: void sleep()
 

      多态(动态多态)条件:1. 发生在基类和派生类之间;2.都是虚函数;3.函数首部要相同(注意:如果返回值类型不同,则会报错啊,我以前不知道这一点,还以为即使返回值不一样,也可以通过晚绑定来实现多态呢!)。

      上面的程序也可以改成引用形式:

 

  1. #include <iostream>
  2. using namespace std;
  3. class Human
  4. {
  5. public:
  6. virtual void eat()
  7. {
  8. cout << "Human: virtual void eat()" << endl;
  9. }
  10. void sleep() // 非虚函数,不具有多态性
  11. {
  12. cout << "Human: void sleep()" << endl;
  13. }
  14. };
  15. class Man : public Human
  16. {
  17. public:
  18. virtual void eat() // 此处的virtual可省略,但最好不要省略
  19. {
  20. cout << "Man: virtual void eat()" << endl;
  21. }
  22. void sleep()
  23. {
  24. cout << "Man: void sleep()" << endl;
  25. }
  26. };
  27. void testEat(Human & b)
  28. {
  29. b.eat();
  30. }
  31. void testSleep(Human & b)
  32. {
  33. b.sleep();
  34. }
  35. int main()
  36. {
  37. Human h;
  38. Man m;
  39. testEat(h);
  40. testSleep(h);
  41. testEat(m);
  42. testSleep(m);
  43. return 0;
  44. }

 

    结果为指针形式的结果一样。在下面的讨论中,我们采用指针的形式,继续看程序:

 

  1. #include <iostream>
  2. using namespace std;
  3. class Human
  4. {
  5. public:
  6. virtual void eat()
  7. {
  8. cout << "Human: virtual void eat()" << endl;
  9. }
  10. };
  11. class Man : public Human
  12. {
  13. public:
  14. // error C2555: 'Man::eat' : overriding virtual function differs
  15. // from 'Human::eat' only by return type or calling convention
  16. virtual int eat()
  17. {
  18. cout << "Man: virtual int eat()" << endl;
  19. }
  20. };
  21. int main()
  22. {
  23. Human *p, h;
  24. Man m;
  25. p = &h;
  26. p->eat();
  27. p = &m;
  28. p->eat();
  29. return 0;
  30. }

     由上面的程序可知,仅仅返回值不同,会报错啊(我以前以为这样也可以有多态性呢)。继续看:

  1. #include <iostream>
  2. using namespace std;
  3. class Human
  4. {
  5. public:
  6. virtual void eat(int a)
  7. {
  8. cout << "Human: virtual void eat(int a)" << endl;
  9. }
  10. };
  11. class Man : public Human
  12. {
  13. public:
  14. virtual void eat(float a) // 不具有多态性
  15. {
  16. cout << "Man: virtual void eat(float a)" << endl;
  17. }
  18. };
  19. int main()
  20. {
  21. Human *p, h;
  22. Man m;
  23. p = &h;
  24. p->eat(1);
  25. p = &m;
  26. p->eat(1);
  27. return 0;
  28. }

     结果为:

 

Human: virtual void eat(int a)
Human: virtual void eat(int a)

     现在,总该对多态有基本的认识了吧。

 

相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载