赞
踩
#include < iostream > #include < string > using namespace std; class Food{ public: Food(char* n) { name = new char[strlen(n) + 1]; strcpy(name,n); } virtual void eat() = 0; protected: char* name; }; class Fruit:public Food { public: Fruit(char* n): Food(n){} void eat() { cout <<"吃"<< name << endl; } }; class Bread:public Food { public: Bread(char* n): Food(n){} void eat() { cout <<"吃"<< name << endl; } }; int main() { int choice; Food * f; Fruit *fr = new Fruit("火龙果"); Bread br("椰三角"); cout << "1: 吃水果\n"; cout << "2: 吃面包\n"; cout << "请输入你的选择:"; cin >> choice; switch(choice) { case 1: f = fr; break; case 2: f = &br; break; } f->eat(); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。