C++中的多态性

C++中的多态性封装、继承、多态是C++的三大基本特性。封装可以隐藏实现细节,使得代码模块化;继承可以扩展已存在的代码模块(类);封装和继承的目的都是为了&qu

大家好,欢迎来到IT知识分享网。

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

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

一、虚函数

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

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

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

下面先看一个例子:

#include <iostream> using namespace std; //基类 class Shape { protected: int width, height; public: Shape( int a=0, int b=0) { width = a; height = b; } int area() { cout << "Parent class area :" <<endl; return 0; } }; //子类Rectangle class Rectangle: public Shape { public: Rectangle( int a=0, int b=0):Shape(a, b) { } int area () { cout << "Rectangle class area :" <<endl; return (width * height); } }; //子类Triangle class Triangle: public Shape { public: Triangle( int a=0, int b=0):Shape(a, b) { } int area () { cout << "Triangle class area :" <<endl; return (width * height / 2); } }; int main( ) { Shape *shape; Rectangle rec(10,7); Triangle tri(10,5); shape = &rec; shape->area(); shape = &tri; shape->area(); return 0; }

运行结果为:

C++中的多态性

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

修改如下:

#include <iostream> using namespace std; //基类 class Shape { protected: int width, height; public: Shape( int a=0, int b=0) { width = a; height = b; } //使用virtual关键字指定为虚函数 virtual int area() { cout << "Parent class area :" <<endl; return 0; } }; //子类Rectangle class Rectangle: public Shape { public: Rectangle( int a=0, int b=0):Shape(a, b) { } virtual int area () { cout << "Rectangle class area :" <<endl; return (width * height); } }; //子类Triangle class Triangle: public Shape { public: Triangle( int a=0, int b=0):Shape(a, b) { } virtual int area () { cout << "Triangle class area :" <<endl; return (width * height / 2); } }; int main( ) { Shape *shape; Rectangle rec(10,7); Triangle tri(10,5); shape = &rec; shape->area(); shape = &tri; shape->area(); return 0; }

执行结果如下:

C++中的多态性

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

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

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

二、纯虚函数

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

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

#include <iostream> using namespace std; class Animal { protected: string name_; public: Animal(string name) { name_ = name; } ~Animal() { } //定义纯虚函数 virtual void ShowMe() = 0; }; class Dog: public Animal { public: Dog(string name): Animal(name) { } ~Dog() { } virtual void ShowMe() { cout << "I am a dog and my name is " << name_ << endl; } }; class Cat: public Animal { public: Cat(string name): Animal(name) { } ~Cat() { } virtual void ShowMe() { cout << "I am a cat and my name is " << name_ << endl; } }; int main() { //error: 抽象类不可以实例化 //Animal* animal = new Animal("动物"); Animal* animal; Dog* dog = new Dog("阿黄"); Cat* cat = new Cat("小黑"); animal = dog; animal->ShowMe(); animal = cat; animal->ShowMe(); return 0; }

运行结果如下:

C++中的多态性

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

C++中的多态性

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/80666.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信