一::
#include <iostream>
using namespace std;
class A
{
public:
void Set(int x,int y){a=x;b=y;}
void Show(){cout<<"a:"<<a<<"/t"<<"b:"<<b<<endl;}
private:
int a;
int b;
};
void (A::*p)(int,int);/*类的成员函数指针*/
int main()
{
A a;
p=&A::Set;
int x=2,y=3;
(a.*p)(x,y);/*(a*p)::p所指向的函数名*/
a.Show();
return 0;
}
二::
#include <iostream>
using namespace std;
class Human
{
public:
virtual void run()=0;
virtual void eat()=0;
};
class Mother:public Human
{
public:
void run(){cout<<"母亲跑米百要花20秒/n";}
void eat(){cout<<"母亲喜欢吃零食/n";}
};
class Father:public Human
{
public:
void run(){cout<<"父亲跑米百要花10秒/n";}
void eat(){cout<<"父亲不喜欢吃零食/n";}
};
class Uncle:public Human
{
public:
void run(){cout<<"舅舅跑米百要花15秒/n";}
void eat(){cout<<"舅舅喜欢偷吃零食/n";}
};
int main()
{
void (Human::*pf)()=0;
Human *p=0;
char choice1,choice2;
bool quit=false;
while (quit==false)
{
cout<<"[0]退出[1]母亲[2]父亲[3]舅舅/n";
cin>>choice1;
switch (choice1)
{
case '0':quit=true;break;
case '1':p=new Mother;break;
case '2':p=new Father;break;
case '3':p=new Uncle;break;
default:choice1='q';break;
}
if (quit==true){break;}
if (choice1=='q'){cout<<"请输入0~3之间的数/n";continue;}
cout<<"[1]跑步[2]进食/n";
cin>>choice2;
switch (choice2)
{
case '1':pf=&Human::run;break;
case '2':pf=&Human::eat;break;
default:break;
}
(p->*pf)();
delete p;
}
return 0;
}