第十七章 特殊成员_类的函数指针
生活随笔
收集整理的這篇文章主要介紹了
第十七章 特殊成员_类的函数指针
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#include <iostream>
using namespace std;
class Human
{
public:virtual void run()=0;virtual void eat()=0;
};class Month:public Human
{
public:void run(){ cout<<"母親跑百米要二十少"<<endl;}void eat(){ cout<<"母親喜歡吃零食"<<endl;}
};class Father:public Human
{
public:void run(){ cout<<"父親跑百米要三十少"<<endl;}void eat(){ cout<<"父親不喜歡吃零食"<<endl;}
};class Uncle:public Human
{
public:void run(){ cout<<"舅舅跑百米要十五少"<<endl;}void eat(){ cout<<"舅舅吃零食"<<endl;}
};int main()
{void (Human::*pf)()=0;Human *p =0;char choice1, choice2;bool quit = false;while(quit == false){cout<<"(0)退出 (1)母親 (2)父親 (3)舅舅"<<endl;cin>>choice1;switch(choice1){case '0':quit=true;break;case '1':p = new Month;break;case '2':p = new Father;break;case '3':p = new Uncle;break;default:choice1='q';break;}if(quit){break;}if(choice1=='q'){cout<<"請輸入0到3之間的數(shù)字"<<endl;continue;}cout<<"(1)跑步 (2)進食"<<endl;cin>>choice2;switch(choice2){case '1':pf=&Human::run;break;case '2':pf = &Human::eat;break;default:continue;}//p指向的是一個類//pf是指向一個類的函數(shù)指針(p->*pf)();delete p;}return 0;
}
總結(jié)
以上是生活随笔為你收集整理的第十七章 特殊成员_类的函数指针的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的MVC之旅(3)--------MV
- 下一篇: css 相关学习