當(dāng)前位置:
首頁 >
C++难题之多态性详细解释
發(fā)布時(shí)間:2025/5/22
43
豆豆
生活随笔
收集整理的這篇文章主要介紹了
C++难题之多态性详细解释
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
C++的多態(tài)性是C++實(shí)現(xiàn)面向?qū)ο蠹夹g(shù)的基礎(chǔ)。具體的說,通過一個(gè)指向基類的指針調(diào)用虛成員函數(shù)的時(shí)候,將能夠根據(jù)指針?biāo)赶虻膶?shí)際對象調(diào)用成員函數(shù)。
#include "stdafx.h" class father { public:void duotai(){printf("調(diào)用的是父函數(shù)\n");}virtual void fun(){printf("調(diào)用的是父虛函數(shù)\n");}}; class son:public father { public:void duotai(){printf("調(diào)用的是子函數(shù)\n");}virtual void fun(){printf("調(diào)用的是子虛函數(shù)\n");} };int main(int argc, char* argv[]) {printf("Hello World!\n");son m_pson;father m_pfather;son *mson=new son;//new 需要new sonfather *mson_father=new son;//new 需要new sonfather *mfather=new father;mson->fun();mson_father->fun();mfather->fun();return 0; }
說明調(diào)用的誰,就是new了誰,給誰創(chuàng)造內(nèi)存空間了
總結(jié)
以上是生活随笔為你收集整理的C++难题之多态性详细解释的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: usb类调用失败解释
- 下一篇: 报错笔记:打开软件出现您缺少mfco42