示例:
#include<iostream>
using namespace std;
class AbstractDrinking
{
public:
//煮水 boil煮水
virtual void Boil()=0;
//沏茶brew
virtual void Brew()=0;
//倒入杯中
virtual void Incup()=0;
// 加入佐料
virtual void PutSomething()=0;
void show()
{
Boil();
Brew();
Incup();
PutSomething();
}
};
class Coffee:public AbstractDrinking
{
public:
void Boil()
{
cout<<"煮露水"<<endl;
}
void Brew()
{
cout<<"沏咖啡"<<endl;
}
void Incup()
{
cout<<"倒入杯中"<<endl;
}
void PutSomething()
{
cout<<"加入牛奶"<<endl;
}
};
class Tea:public AbstractDrinking
{
public:
void Boil()
{
cout<<"煮水"<<endl;
}
void Brew()
{
cout<<"沏茶"<<endl;
}
void Incup()
{
cout<<"倒入杯中"<<endl;
}
void PutSomething()
{
cout<<"加入柠檬"<<endl;
}
};
void test(AbstractDrinking *pre)//这里相当于AbstractDrinking *per=new Coffee
{
pre->show();
return ;
}
void test01()
{
test(new Coffee);
cout<<"*****************"<<endl;
test(new Tea);
}
int main()
{
test01();
return 0;