对象3:继承
#include <iostream> #include <string> using namespace std; class Animal { public: string mouth; void eat(); void sleep(); void drool(); }; class Pig : public Animal { public: void climb(); }; class Turtle : public Animal { public: void swim(); }; void Animal::eat() { cout << "I am eating " << endl; } void Animal::sleep() { cout << "I am sleeping , don't disturb me " << endl; } void Animal::drool() { cout << " i am female , when i look male , i will flow water" << endl; } void Pig::climb() { cout << " 我是一个漂亮的母猪, 我会上树, 我正在上树" << endl; } void Turtle::swim() { cout << "我是一只小甲鱼,当母猪要来抓我的时候,我就游到海里。。 哈哈" << endl; } int main() { Pig pig; Turtle turtle; pig.eat(); turtle.eat(); pig.climb(); turtle.swim(); return 0; }