diff --git a/.gitignore b/.gitignore index 131b6ff..07316dc 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,8 @@ *.tmproject tmtags +Makefile +*.cpp +*.hpp +*.exe +*.json diff --git a/Chapter2 C与C++/2.10 类、继承和多态.md b/Chapter2 C与C++/2.10 类、继承和多态.md index fe7c2b3..3a1a1cb 100644 --- a/Chapter2 C与C++/2.10 类、继承和多态.md +++ b/Chapter2 C与C++/2.10 类、继承和多态.md @@ -1,75 +1,184 @@ # 2.10 类、继承和多态 -## 2.10.1 类 +## 2.10.1 类和多态 -在 C++ 中,我们通过如下方法定义和实现一个类: +在 C++ 中,我们通过如下方法定义和实现一个类: ```cpp /** - * @file   Tree.h + * @file   Animal.hpp */ -class Tree +#pragma once // 只被编译一次. + +#include // 使用 C++ 的 string 类. +using namespace std; // 使用 std 命名空间. + +class Animal { public: -   Tree(); - ~Tree(); + Animal(); // 构造函数. + virtual ~Animal(); // 析构函数. + virtual void Sound(void); -   char Color[3]; -   double Height; -   double Width; + char Color[3]; // 属性. + double Height; + double Width; -protect: -   virtual void Growth(void); -   virtual void Growth(double rate); +protected: + virtual void Growth(void); // 方法. + virtual void Growth(double rate); -   string Shape; + string Sounds; private: -   void Haha(void); + void MakeSounds(void); -   long Nothing; + string Shape; + +}; -} ``` -对应的实现为: +可以看到,Animal 类中有两个参数不同的 Growth 方法,这被称作多态,在使用 Animal 对象时,依据调用 Growth 方法时传入的参数个数和类型来判断具体使用哪个 Growth 方法。Animal 类的实现为: ```cpp /** - * @file   Tree.cpp + * @file   Animal.cpp */ -#include +#include "Animal.hpp" +#include -using namespace std; - -Tree::Tree() +Animal::Animal() { -   this->Height = 8.5; + this->Height = 8.5; + this->Sounds = "Bebe..."; } -Tree::~Tree() +Animal::~Animal() { cout<<"Dead."<MakeSounds(); } -void Tree::Growth(double rate) +void Animal::Growth(void) { -   Height *= (1+rate); + Height += 0.2; + Width += 0.1; +} + +void Animal::Growth(double rate) +{ + Height *= (1+rate); Width *= (1+rate); } -void Tree::Haha(void) +void Animal::MakeSounds(void) { -   cout<<"Nothing to do."< + +Dog::Dog() +{ + this->Height = 0.6; + this->Sounds = "Wang!Wang!Wang!"; +} + +Dog::~Dog() +{ + cout<<"Dog Dead. Wu...Wu...T_T..."< +#include + +int main() { + Animal* animal; + animal = new Animal(); + animal->Color[1] = 255; + 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. + return 0; +} +``` + +通过 dog 调用 Growth 方法那句,更能体现类抽象的实质。 + +## 练习 + +编写“人类”继承于 Animal 类,扩充其“姓名”和“性别”属性,编写“学生”类继承于“人类”,扩充其“成绩”属性,基于这些类,实现学生成绩录入和现实系统。 \ No newline at end of file diff --git a/Chapter2 C与C++/2.9 面向过程与面向对象.md b/Chapter2 C与C++/2.9 面向过程与面向对象.md index f86beba..8950f78 100644 --- a/Chapter2 C与C++/2.9 面向过程与面向对象.md +++ b/Chapter2 C与C++/2.9 面向过程与面向对象.md @@ -45,6 +45,5 @@ class 的概念与现实中的类概念一致,它是具有相同特性群体 C++ 是面向对象思想的最佳实现。它扩充了 C 语言,并降低了二次学习的成本。C++ 非常完整的继承了面向对象思想,并在语言层面提供了面向对象工具。在 C++ 类内部,可以有多个同名方法,但他们的参数个数或参数类型不同,这一特性,被称作多态。 ## 练习 + 以身边事物为参考,举出父类和多个子类的例子,并说出哪些是属性,哪些是方法,哪里提现出了继承,覆盖和多态思想。 - -