#include<iostream>
using namespace std;
class Base {
public:
virtual void boil() = 0;
virtual void brew() = 0;
virtual void putintocup() = 0;
virtual void putinsomething() = 0;
void makedtink() {
boil() ;
brew();
putintocup() ;
putinsomething() ;
}
};
class coffe :public Base {
virtual void boil() {
cout << "煮矿泉水" << endl;
};
virtual void brew() {
cout << "冲泡咖啡" << endl;
};
virtual void putintocup() {
cout << "倒入杯中" << endl;
};
virtual void putinsomething(){
cout << "加入糖和牛奶" << endl;
};
};
class tea :public Base {
virtual void boil() {
cout << "煮矿泉水" << endl;
};
virtual void brew() {
cout << "冲泡茶" << endl;
};
virtual void putintocup() {
cout << "倒入杯中" << endl;
};
virtual void putinsomething() {
cout << "加入枸杞" << endl;
};
};
void dowork(Base* a) {//Base *a=new coffe
a->makedtink();
delete a;
}
void test() {
dowork(new coffe);
cout << "-----------" << endl;
dowork(new tea);
}
int main() {
test();
return 0;
}
黑马-多态-案例2-制作饮品类
最新推荐文章于 2024-09-10 19:16:08 发布