请设计以下食草动物、马、驴、骡等 4 个类。
-
HERB 为食草动物类,是公共基类,具有食草动物的基本特性。包括:
一个保护数据成员:appet(食量)。
三个公有成员函数:构造函数(初始化食量)、析构函数和 Show 函数(显示食量)。
-
HORSE 为马类,以公有方式继承 HERB 类,具有食草动物的特性,还能跑得很快。包括:
一个保护数据成员:speed(速度)。
三个公有成员函数:构造函数、析构函数和 Show 函数(显示食量和速度)。
-
DONKEY 为驴类,以公有方式继承 HERB 类,具有食草动物的特性,还能驼重物。包括:
一个保护数据成员:load(负重)。
三个公有成员函数:构造函数、析构函数和 Show 函数(显示食量和负重)。
-
MULE 为骡类,同时以公有方式继承 HORSE 类和 DONKEY 类,兼具马和驴的特性。包括:
三个公有成员函数:构造函数、析构函数和 Show 函数(显示食量、速度和负重)。
测试用例
- 样例一
HERB *p; p = new HERB(2.1); p->Show(); delete p;
A herb is born.
I need 2.1 kg food.
A herb is dead.
- 样例二
HERB *p; p = new HORSE(4.5, 58.2); p->Show(); delete p;
A herb is born.
A horse is born.
I need 4.5 kg food, I can run at 58.2 km/h.
A horse is dead.
A herb is dead.
- 样例三
HERB *p; p = new DONKEY(2.4, 213.7); p->Show(); delete p;
A herb is born.
A donkey is born.
I need 2.4 kg food, I can carry 213.7 kg load.
A donkey is dead.
A herb is dead.
- 样例四
HERB *p; p = new MULE(2.8, 46.7, 208.9); p->Show(); delete p;
A herb is born.
A horse is born.
A donkey is born.
A mule is born.
I need 2.8 kg food, I can run at 46.7 km/h, I can carry 208.9 kg load.
A mule is dead.
A donkey is dead.
A horse is dead.
A herb is dead.
裁判程序
#include <iostream> using namespace std; /* 你提交的代码将被嵌在这里 */ int main() { double a, s, l; HERB *p; cin >> a >> s >> l; p = new MULE(a, s, l); p->Show(); delete p; return 0; }
输入样例
2.1 54.6 198.3
输出样例
A herb is born.
A horse is born.
A donkey is born.
A mule is born.
I need 2.1 kg food, I can run at 54.6 km/h, I can carry 198.3 kg load.
A mule is dead.
A donkey is dead.
A horse is dead.
A herb is dead.
答案1:
class HERB {
protected:
double appet; // 食量
public:
// 构造函数
HERB(double a) : appet(a) {
cout << "A herb is born." << endl;
}
// 析构函数
virtual ~HERB() {
cout << "A herb is dead." << endl;
}
// 显示食量
virtual void Show() const {
cout << "I need " << appet << " kg food." << endl;
}
};
// 马类
class HORSE : virtual public HERB {
protected:
double speed; // 速度
public:
// 构造函数
HORSE(double a, double s) : HERB(a), speed(s) {
cout << "A horse is born." << endl;
}
// 析构函数
virtual ~HORSE() {
cout << "A horse is dead." << endl;
}
// 显示食量和速度
virtual void Show() const {
cout << "I need " << appet << " kg food, I can run at " << speed << " km/h." << endl;
}
};
// 驴类
class DONKEY : virtual public HERB {
protected:
double load; // 负重
public:
// 构造函数
DONKEY(double a, double l) : HERB(a), load(l) {
cout << "A donkey is born." << endl;
}
// 析构函数
virtual ~DONKEY() {
cout << "A donkey is dead." << endl;
}
// 显示食量和负重
virtual void Show() const {
cout << "I need " << appet << " kg food, I can carry " << load << " kg load." << endl;
}
};
// 骡类
class MULE : public HORSE, public DONKEY {
public:
// 构造函数
MULE(double a, double s, double l) : HERB(a), HORSE(a, s), DONKEY(a, l) {
cout << "A mule is born." << endl;
}
// 析构函数
virtual ~MULE() {
cout << "A mule is dead." << endl;
}
// 显示食量、速度和负重
virtual void Show() const {
cout << "I need " << appet << " kg food, I can run at " << speed << " km/h, I can carry " << load << " kg load." << endl;
}
};
答案2纯净版:
#include <iostream>
using namespace std;
class HERB
{
protected:
double appet;
public:
HERB(double a):appet(a)
{
cout<<"A herb is born."<<endl;
}
virtual ~HERB()
{
cout<<"A herb is dead."<<endl;
}
virtual void Show() const
{
cout<<"I need "<<appet<<" kg food."<<endl;
}
};
class HORSE : virtual public HERB
{
protected:
double speed;
public:
HORSE(double a,double s):HERB(a),speed(s)
{
cout<<"A horse is born."<<endl;
}
virtual ~HORSE()
{
cout<<"A horse is dead."<<endl;
}
virtual void Show() const
{
cout << "I need " << appet << " kg food, I can run at " << speed << " km/h." << endl;
};
};
class DONKEY : virtual public HERB
{
protected:
double load;
public:
DONKEY(double a,double l):HERB(a),load(l)
{
cout<<"A donkey is born."<<endl;
}
virtual ~DONKEY()
{
cout<<"A donkey is dead."<<endl;
}
virtual void Show() const
{
cout << "I need " << appet << " kg food, I can carry " << load << " kg load." << endl;
}
};
class MULE :public HORSE,public DONKEY
{
public:
MULE(double a,double s,double l):HERB(a), HORSE(a, s), DONKEY(a, l)
{
cout<<"A mule is born."<<endl;
}
virtual ~MULE()
{
cout<<"A mule is dead."<<endl;
}
virtual void Show() const
{
cout << "I need " << appet << " kg food, I can run at " << speed << " km/h, I can carry " << load << " kg load." << endl;
}
};