C++中的多态性

封装、继承、多态是C++的三大基本特性。封装可以隐藏实现细节,使得代码模块化;继承可以扩展已存在的代码模块(类);封装和继承的目的都是为了"代码重用",多态则是为了实现另一个目的:接口重用。

多态说的简单一些就是"相同的调用产生不同的行为"。这句话具体的涵义在下面会通过例子来解释。

一、虚函数

C++中的多态是通过虚函数来实现的。虚函数的作用是允许子类重新定义父类的成员函数,这种行为称为覆盖或者重写。

(1)如果使用了多态,利用基类的指针指向任意一个子类对象,调用相应的虚函数(可调用到子类重写的父类中定义的虚函数),从而实现动态绑定。

(2)如果没有使用虚函数,那么即使在子类中存在与父类同名的函数,基于指针也只能调用基类的该成员函数,无法调用到子类中被重写的该成员函数。

下面先看一个例子:

#include <iostream> 
using namespace std;

//基类
class Shape {
protected:
\tint width, height;

public:
\tShape( int a=0, int b=0) {
\t\twidth = a;
\t\theight = b;
\t}

\tint area() {
\t\tcout << "Parent class area :" <<endl>\t\treturn 0;
\t}
};

//子类Rectangle
class Rectangle: public Shape {
public:
\tRectangle( int a=0, int b=0):Shape(a, b) {
\t
\t}
\tint area () {
\t\tcout << "Rectangle class area :" <<endl>\t\treturn (width * height);
\t}
};

//子类Triangle
class Triangle: public Shape {
public:
\tTriangle( int a=0, int b=0):Shape(a, b) {
\t
\t}
\tint area () {
\t\tcout << "Triangle class area :" <<endl>\t\treturn (width * height / 2);
\t}
};

int main( ) {
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);

shape = &rec;
shape->area();

shape = &tri;
shape->area();

return 0;
}/<endl>/<endl>/<endl>/<iostream>

运行结果为:

C++中的多态性

可以看到即使是将指针shape指向不同的子类(Rectangle/Triangle),执行shape->area();调用的仍然还是父类Shape的成员函数area();。并没有产生上面所说的"多态"的行为。

修改如下:

#include <iostream> 
using namespace std;

//基类
class Shape {
protected:
\tint width, height;

public:
\tShape( int a=0, int b=0) {
\t\twidth = a;
\t\theight = b;
\t}
\t//使用virtual关键字指定为虚函数
\tvirtual int area() {
\t\tcout << "Parent class area :" <<endl>\t\treturn 0;
\t}
};

//子类Rectangle
class Rectangle: public Shape {
public:
\tRectangle( int a=0, int b=0):Shape(a, b) {
\t
\t}
\tvirtual int area () {
\t\tcout << "Rectangle class area :" <<endl>\t\treturn (width * height);
\t}
};

//子类Triangle
class Triangle: public Shape {
public:
\tTriangle( int a=0, int b=0):Shape(a, b) {
\t
\t}
\tvirtual int area () {

\t\tcout << "Triangle class area :" <<endl>\t\treturn (width * height / 2);
\t}
};

int main( ) {
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);

shape = &rec;
shape->area();

shape = &tri;
shape->area();

return 0;
}/<endl>/<endl>/<endl>/<iostream>

执行结果如下:

C++中的多态性

相同的调用shape->area();产生了不同的行为,这个现象就是上面所说的多态性。

下面对虚函数给出一个定义:

虚函数是在基类中使用关键字virtual声明的函数。在子类中重写基类中定义的虚函数时,会告诉编译器不要静态链接到该函数,而是在程序运行的任何时候根据指针所指向的对象来选择调用的函数,这种操作被称为"动态绑定或后期绑定"。

二、纯虚函数

  • 纯虚函数是在基类中声明的虚函数,它没有具体的定义,要求任何派生类都必须重写纯虚函数
  • 包含纯虚函数的类称为抽象类,抽象类不可以进行实例化
  • 定义纯虚函数是为了实现一个接口,起到一个规范的作用,它规定继承这个类的程序员必须要重写该纯虚函数

为什么需要纯虚函数:在很多情况下,基类本身生成对象时不合情理的。比如说:动物作为一个基类可以派生出老虎类、猫类、狗类,但是动物类本身实例化对象就不合常理。因此引入了纯虚函数,包含纯虚函数的类称为抽象类,用户不能创建抽象类的实例,只能创建它的派生类的实例。

#include <iostream>
using namespace std;

class Animal {
protected:
\tstring name_;
public:
\tAnimal(string name) {
\t\tname_ = name;
\t}
\t~Animal() {
\t\t
\t}
//定义纯虚函数
\tvirtual void ShowMe() = 0;
};

class Dog: public Animal {
public:
\tDog(string name): Animal(name) {
\t\t
\t}
\t~Dog() {
\t\t
\t}
\tvirtual void ShowMe() {
\t\tcout << "I am a dog and my name is " << name_ << endl;
\t}
};

class Cat: public Animal {
public:
\tCat(string name): Animal(name) {
\t\t
\t}
\t~Cat() {
\t\t
\t}
\tvirtual void ShowMe() {
\t\tcout << "I am a cat and my name is " << name_ << endl;
\t}
};

int main() {
\t//error: 抽象类不可以实例化
\t//Animal* animal = new Animal("动物");\t
\tAnimal* animal;
\tDog* dog = new Dog("阿黄");

\tCat* cat = new Cat("小黑");
\t
\tanimal = dog;
\tanimal->ShowMe();
\t
\tanimal = cat;
\tanimal->ShowMe();
\t
\treturn 0;
}/<iostream>

运行结果如下:

C++中的多态性

今天的内容就到这儿了。如果对我的推|文有兴趣,欢迎转|载分|享。也可以推|荐给朋友关|注哦。只推干货,宁缺毋滥。

C++中的多态性


分享到:


相關文章: