//多态实现一个饮料制作
//n的全排列
#include<iostream>
using namespace std;
class abstract
{
public:virtual void makedrinking() = 0;
};
class makecoffee :public abstract
{
public: virtual void makedrinking()
{
cout << "第一步:烧水" << endl;
cout << "第二步:倒入杯中" << endl;
cout << "第三步:添加佐料" << endl;
};
};
void doworks(abstract& abc)//使用引用的方法指向子类成员 需要创建新的对象
{
abc.makedrinking();
}
void test02()
{
makecoffee coffee;
doworks(coffee);
}
void dowork(abstract*abc)//使用指针指向子类成员时 需要new一个子类 new创建的是地址
{
abc->makedrinking();
}
void test01()
{
dowork(new makecoffee);
}
int main()
{
test01();
cout << endl;
test02();
return 0;
system("pause");
}
06-14
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交