欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

C++中多态的概念和意义

发布时间:2025/4/5 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C++中多态的概念和意义 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

    • 1 C++中多态的概念和意义
      • 1.1 函数重写回顾
      • 1.2 多态的概念
      • 1.3 多态的意义
      • 1.4 静态联编和动态联编
    • 2 特殊的虚函数
      • 2.1 构造函数能成为虚函数吗
      • 2.2 析构函数能成为虚函数吗
    • 3 构造函数和析构函数中的多态

1 C++中多态的概念和意义

1.1 函数重写回顾

对于函数重写:

  • 父类被重写的函数依然会继承给子类。
  • 子类中重写的函数将覆盖父类中的函数。
  • 通过作用域分辨符(::)可以访问到父类中的函数。

1.2 多态的概念

面向对象中期望的行为:

  • 根据实际的对象类型判断如何调用重写函数。
  • 父类指针(引用)指向:
    • 父类对象则调用父类中定义的函数。
    • 子类对象则调用子类中定义的重写函数。

面向对象中多态的概念:

  • 过呢据实际的对象类型决定函数调用的具体目标。
  • 同样的调用语句在实际运行时有多种不同的表现形态。

C++语言直接支持多态的概念:

  • 通过virtua关键字对多态进行支持。
  • 被virtual声明的函数被重写后具有多态特性。
  • 被virtual声明的函数叫做虚函数。

多态示例:

#include <iostream> #include <string>using namespace std;class Parent { public:virtual void print(){cout << "I'm Parent." << endl;} };class Child : public Parent { public:void print(){cout << "I'm Child." << endl;} };void how_to_print(Parent* p) {p->print(); // 展现多态的行为 }int main() {Parent p;Child c;how_to_print(&p); // Expected to print: I'm Parent.how_to_print(&c); // Expected to print: I'm Child.return 0; }

1.3 多态的意义

多态的意义:

  • 在程序运行过程中展现出动态的特性。
  • 函数重写必须多态实现,否则没有意义。
  • 多态是面向对象组件化程序设计的基础特性。

1.4 静态联编和动态联编

静态联编:

  • 在程序的编译期间就能确定具体的函数调用,如:函数重载。

动态联编:

  • 在程序实际运行后才能确定具体的函数调用,如:函数重写。

动态联编与静态联编:

#include <iostream> #include <string>using namespace std;class Parent { public:virtual void func(){cout << "void func()" << endl;}virtual void func(int i){cout << "void func(int i) : " << i << endl;}virtual void func(int i, int j){cout << "void func(int i, int j) : " << "(" << i << ", " << j << ")" << endl;} };class Child : public Parent { public:void func(int i, int j){cout << "void func(int i, int j) : " << i + j << endl;}void func(int i, int j, int k){cout << "void func(int i, int j, int k) : " << i + j + k << endl;} };void run(Parent* p) {p->func(1, 2); // 展现多态的特性// 动态联编 }int main() {Parent p;p.func(); // 静态联编p.func(1); // 静态联编p.func(1, 2); // 静态联编cout << endl;Child c;c.func(1, 2); // 静态联编cout << endl;run(&p);run(&c);return 0; }

2 特殊的虚函数

2.1 构造函数能成为虚函数吗

构造函数不可能成为虚函数:

  • 在构造函数结束后,虚函数表指针才会被正确的初始化。

2.2 析构函数能成为虚函数吗

析构函数可以成为虚函数:

  • 建议在设计类时将析构函数声明为虚函数(发生赋值兼容时,可以正确的调用析构函数,防止内存泄漏的发生)。
#include <iostream> #include <string>using namespace std;class Base { public:Base(){cout << "Base()" << endl;func();}virtual void func() {cout << "Base::func()" << endl;}virtual ~Base(){func();cout << "~Base()" << endl;} };class Derived : public Base { public:Derived(){cout << "Derived()" << endl;func();}virtual void func(){cout << "Derived::func()" << endl;}~Derived(){func();cout << "~Derived()" << endl;} };int main() {Base* p = new Derived();// ...delete p;return 0; }

输出结果:


3 构造函数和析构函数中的多态

析构函数中是否可以发生多态?
析构函数中是否可以发生多态?

构造函数中不可能发生多态行为:

  • 在构造函数执行时,虚函数表指针未被正确初始化。

析构函数中不可能发生多态行为:

  • 在析构函数执行时,虚函数表指针已经被销毁。

构造函数和析构函数中不能发生多态行为,只能调用当前类中定义的函数版本!

示例程序见2.2中的示例程序。


参考资料:

  • C++深度解析教程
  • 总结

    以上是生活随笔为你收集整理的C++中多态的概念和意义的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。