qfedu-cpp-level/day5/homework/h5.cpp

43 lines
1010 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 假设有三个类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;
}