C++难题之多态性详细解释
生活随笔
收集整理的這篇文章主要介紹了
C++难题之多态性详细解释
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C++的多態性是C++實現面向對象技術的基礎。具體的說,通過一個指向基類的指針調用虛成員函數的時候,將能夠根據指針所指向的實際對象調用成員函數。
#include "stdafx.h" class father { public:void duotai(){printf("調用的是父函數\n");}virtual void fun(){printf("調用的是父虛函數\n");}}; class son:public father { public:void duotai(){printf("調用的是子函數\n");}virtual void fun(){printf("調用的是子虛函數\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; }
說明調用的誰,就是new了誰,給誰創造內存空間了
總結
以上是生活随笔為你收集整理的C++难题之多态性详细解释的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: usb类调用失败解释
- 下一篇: MFC EDIT控件的使用记录