实例化对象的两种方法:
base a;
new base;
多态调用时:父类为虚函数,则无法实例化,
bug:
调用函数时不用加返回值如void;否则不会调用
注意一定要加public:
#include#include using namespace std; //制作饮品类 class DoDrink { public: //煮水; virtual void Boil() = 0; //冲泡; virtual void Brew() = 0; //倒入杯中 virtual void PourIn() = 0; //加入辅料; virtual void AddSomething() = 0; //制作流程; void DoWork() { Boil(); Brew(); PourIn(); AddSomething(); } }; //制作咖啡 class Coffee:public DoDrink { public: //煮水; void Boil() { cout << "加入农夫山泉" << endl; } //冲泡; virtual void Brew() { cout << "冲泡咖啡" << endl; } //倒入杯中 virtual void PourIn() { cout << "倒入杯中" << endl; } //加入辅料; virtual void AddSomething() { cout << "加入奶粉" << endl; } }; void Make(DoDrink* abs) { abs->DoWork(); } int main() { DoDrink* c = new Coffee; c->DoWork(); delete c; //第二种调用; //Make(new Coffee); system("pause"); return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)