C++中的多态行为

本文参考了下列博客:
1. C++封装、继承、多态
2. C++ protected访问权限问题

封装、继承、多态是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;
}

运行结果如下:
1

可以看到即使是将指针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;
}

执行结果如下:
在这里插入图片描述

增加了virtual关键字后就产生了上面所说的多态的行为。

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

虚函数是在基类中使用关键字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;
}

运行结果:
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值