43 lines
1010 B
C++
43 lines
1010 B
C++
// 假设有三个类,Animal(动物),Mammal(哺乳动物)和 Dog(狗)。Animal 类具有一个成员函数 eat() ,用于输出动物吃的食物。Mammal 类继承自 Animal 类,并添加了一个成员函数 giveBirth() ,用于输出哺乳动物的生育方式。Dog 类继承自 Mammal 类,并添加了一个成员函数 bark() ,用于输出狗的叫声。请在给定的类定义中完成代码,并实现相应的成员函数。
|
||
#include <iostream>
|
||
#include <cstring>
|
||
#include <cstdlib>
|
||
|
||
using namespace std;
|
||
|
||
class Animal
|
||
{
|
||
public:
|
||
void eat()
|
||
{
|
||
cout << "Animal eat" << endl;
|
||
}
|
||
};
|
||
|
||
class Mammal : public Animal
|
||
{
|
||
public:
|
||
void giveBirth()
|
||
{
|
||
cout << "Mammal give birth" << endl;
|
||
}
|
||
};
|
||
|
||
class Dog : public Mammal
|
||
{
|
||
public:
|
||
void bark()
|
||
{
|
||
cout << "Dog bark" << endl;
|
||
}
|
||
};
|
||
|
||
int main()
|
||
{
|
||
Dog dog;
|
||
dog.eat(); // Animal eat
|
||
dog.giveBirth(); // Mammal give birth
|
||
dog.bark(); // Dog bark
|
||
return 0;
|
||
}
|