抽象类Mammal有纯虚函数graze()。Mammal类派生出Cow类和Sheep类,…
- User
抽象类Mammal有纯虚函数graze()。Mammal类派生出Cow类和Sheep类, Cow类扩展数据成员名称(字符串),Sheep类扩展数据成员名称(字符串)。声明一个Mammal类的对象,通过此对象调用graze函数。 参考主函数: int main() { string name; cin >> name; Cow cc(name); cin >> name; Sheep ss(name); Mammal* pm; pm = &cc; pm->graze(); pm = &ss; pm->graze(); return 0; } 【输入形式】 【输出形式】 【样例输入】 牛牛 羊羊 【样例输出】 牛牛在吃草! 羊羊在吃草! 【样例输入】 老虎 小鸡 【样例输出】 老虎在吃草! 小鸡在吃草!
- Assistant
#include #include using namespace std;
class Mammal{ public: virtual void graze() = 0; //纯虚函数 };
class Cow: public Mammal{ public: Cow(string n):name(n){} void graze(){ cout << name << "在吃草!" << endl; } private: string name; };
class Sheep: public Mammal{ public: Sheep(string n):name(n){} void graze(){ cout << name << "在吃草!" << endl; } private: string name; };
int main(){ string name; cin >> name; Cow cc(name); cin >> name; Sheep ss(name); Mammal* pm; pm = &cc; pm->graze(); pm = &ss; pm->graze(); return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.