类的继承: 当使用class Pig:public Animal{} 就可以继承Animal里面的属性,
类的函数改写: 对于继承父类的属性,可以在子类里面重新被定义和改写
#include <iostream> class Animal{ public: std::string mouth; Animal(); ~Animal(); void eat(); void sleep(); void drool(); }; Animal::Animal() { std::cout << "请开始你的表演" << std::endl; } Animal::~Animal() { std::cout << "游戏结束" << std::endl; } void Animal::eat() { std::cout << "正在吃饭" << std::endl; } void Animal::sleep() { std::cout << "我正在吃饭" << std::endl; } void Animal::drool() { std::cout << "我正在流口水" << std::endl; } class Pig : public Animal { public: void eat(); void climb(); }; void Pig::climb() { std::cout << "我是猪我会爬树" << std::endl; } void Pig::eat() { std::cout << "猪正在吃饭" << std::endl; } class Turble : public Animal { public: void eat(); void swim(); }; void Turble::swim() { std::cout << "小乌龟正在游泳" << std::endl; } void Turble::eat() { std::cout << "小乌龟正在吃饭" << std::endl; } int main() { Turble turble; Pig pig; turble.eat(); turble.swim(); pig.eat(); pig.climb(); }