• 虚函数(多态)


    多态:

    静态多态---->通过函数重载实现

    动态多态------>通过虚函数实现

    什么是虚函数

    虚函数不是说在基类中定义一个,然后再在派生类中再定义一个和基类除了函数体不同,其他都相同的函数就是虚函数。

    虚函数是将基类中需要在派生类重新构造的同名函数加上virtual关键字。

    虚函数使用过程中的构造函数初始化问题

    连接

    1,多重继承--->只需对直接基类的构造函数初始化。

    2,虚基类------->对于虚基类的继承方式,所有基类都由派生类用初始化列表初始化。

    虚函数的作用

    可以通过基类指针或者引用来访问基类和派生类的这些同名函数。

    1. #pragma once
    2. #include <iostream>
    3. using namespace std;
    4. class animal
    5. {
    6. public:
    7. int foot;
    8. animal(int f) :foot(f) {}
    9. void print()
    10. {
    11. cout << "animal_foot:"<<foot << endl;
    12. }
    13. };
    14. class bird :public animal
    15. {
    16. public:
    17. int weight;
    18. bird(int f, int w) :animal(f), weight(w) {}
    19. void print()
    20. {
    21. cout << "bird_foot:" <<foot<<"bird_wieght:"<<weight << endl;
    22. }
    23. };
    24. class sparrow :public bird
    25. {
    26. public:
    27. int fly_height;
    28. sparrow(int f, int w, int h) :bird(f,w), fly_height(h) {}
    29. void print()
    30. {
    31. cout << "sparrow_foot:" << foot << "sparrow_weight:" << weight << "sparrow_fly_heigth:" << fly_height << endl;
    32. }
    33. };
    34. #include "base.h"
    35. int main()
    36. {
    37. animal an(4);
    38. bird bd(2,20);
    39. sparrow sp(2, 20, 300);
    40. animal* p = &an;
    41. p->print();
    42. p = &bd;
    43. p->print();
    44. p = &sp;
    45. p->print();
    46. return 0;
    47. }

    输出:

     结论:如果不是虚函数,则派生类给基类指针或者引用赋值,就只是赋值派生类含有的基类部分。

    只需要在第一个类的同名函数之前加上一个virtual之后:

    1. #pragma once
    2. #include <iostream>
    3. using namespace std;
    4. class animal
    5. {
    6. public:
    7. int foot;
    8. animal(int f) :foot(f) {}
    9. virtual void print()
    10. {
    11. cout << "animal_foot:"<<foot << endl;
    12. }
    13. };
    14. class bird :public animal
    15. {
    16. public:
    17. int weight;
    18. bird(int f, int w) :animal(f), weight(w) {}
    19. void print()
    20. {
    21. cout << "bird_foot:" <<foot<<" bird_wieght:"<<weight << endl;
    22. }
    23. };
    24. class sparrow :public bird
    25. {
    26. public:
    27. int fly_height;
    28. sparrow(int f, int w, int h) :bird(f,w), fly_height(h) {}
    29. void print()
    30. {
    31. cout << "sparrow_foot:" << foot << " sparrow_weight:" << weight << " sparrow_fly_heigth:" << fly_height << endl;
    32. }
    33. };
    34. #include "base.h"
    35. int main()
    36. {
    37. animal an(4);
    38. bird bd(2,20);
    39. sparrow sp(2, 20, 300);
    40. animal* p = &an;
    41. p->print();
    42. p = &bd;
    43. p->print();
    44. p = &sp;
    45. p->print();
    46. return 0;
    47. }

    输出:

     结论:

    虚函数的作用

    可以通过基类指针或者引用来访问基类和派生类的这些同名函数。

    基类指针和引用不仅可以访问基类同名函数,也可以访问派生类的同名函数。

    虚函数原理

    注意:一个类(不是类对象)含有一个虚函数表,而一个类对象含有一个虚表指针。

    建立对象,执行构造函数时,将这个虚函数表(数组)的地址赋给对象的虚表指针,通过虚表指针就可以访问到指定类的虚函数。

    几个虚函数原理文章

    文章1

    文章2 

  • 相关阅读:
    电脑监控软件内网版和外网版哪个好?
    (附源码)计算机毕业设计SSM居民个人健康服务平台
    【紫光同创国产FPGA教程】——PDS安装教程
    17.linuxGPIO应用编程
    Makefile+OpenOCD开发STM32
    Feign远程接口调用
    在emacs中,设置latex的主文档
    三栏侧边栏的实现
    操作系统学习笔记12 | 从生磁盘到文件
    suricata 流管理
  • 原文地址:https://blog.csdn.net/m0_60274660/article/details/125627910