diff --git a/Chapter2_C与C++/2.10_类、继承和多态.md b/Chapter2_C与C++/2.10_类、继承和多态.md index f09b3ba..c515a40 100644 --- a/Chapter2_C与C++/2.10_类、继承和多态.md +++ b/Chapter2_C与C++/2.10_类、继承和多态.md @@ -90,17 +90,17 @@ void Animal::MakeSounds(void) ```cpp /** - * @file   Dog.hpp + * @file   Puppy.hpp */ #pragma once #include "Animal.hpp" -class Dog : public Animal +class Puppy : public Animal { public: - Dog(); - virtual ~Dog(); + Puppy(); + virtual ~Puppy(); protected: virtual void Growth(void); @@ -110,46 +110,46 @@ protected: }; ``` -可以看到,Dog 类覆盖了 Animal 类中两个 Growth 方法,并且通过多态扩充了一个 Growth 方法。看下 Dog 类的实现: +可以看到,Puppy 类覆盖了 Animal 类中两个 Growth 方法,并且通过多态扩充了一个 Growth 方法。看下 Puppy 类的实现: ```cpp /** - * @file   Dog.cpp + * @file   Puppy.cpp */ -#include "Dog.hpp" +#include "Puppy.hpp" #include -Dog::Dog() +Puppy::Puppy() { this->Height = 0.6; this->Sounds = "Wang!Wang!Wang!"; } -Dog::~Dog() +Puppy::~Puppy() { - cout<<"Dog Dead. Wu...Wu...T_T..."< -#include +#include int main() { Animal* animal; @@ -167,17 +167,17 @@ int main() { animal->Sound(); // 输出 Bebe... delete animal; // 输出 Dead. - Dog* dog; - dog = new Dog(); - dog->Color[0] = 255; - dog->Growth(0.2); // 调用 Animal 类中的 void Growth(double rate) 方法. - dog->Sound(); // 输出 Wang!Wang!Wang! - delete dog; // 输出 Dog Dead. Wu...Wu...T_T... 和 Dead. + Puppy* puppy; + puppy = new Puppy(); + puppy->Color[0] = 255; + puppy->Growth(0.2); // 调用 Animal 类中的 void Growth(double rate) 方法. + puppy->Sound(); // 输出 Wang!Wang!Wang! + delete puppy; // 输出 Puppy Dead. Wu...Wu...T_T... 和 Dead. return 0; } ``` -通过 dog 调用 Growth 方法那句,更能体现类抽象的实质。 +通过 puppy 调用 Growth 方法那句,更能体现类抽象的实质。 ## 练习