运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 Spaces
#include <iostream> #include <string> using namespace std; // Base class class Animal { public: void animalSound() { cout << "The animal makes a sound \n"; } }; // Derived class class Pig : public Animal { public: void animalSound() { cout << "The pig says: wee wee \n"; } }; // Derived class class Dog : public Animal { public: void animalSound() { cout << "The dog says: bow wow \n"; } }; int main() { Animal myAnimal; Pig myPig; Dog myDog; myAnimal.animalSound(); myPig.animalSound(); myDog.animalSound(); return 0; }
动物发出声音
猪说: wee wee
狗说: bow wow