c++的多态性分为静态多态性与动态多态性。比如函数重载,一个函数名给出不同的参数,实现功能上大体相同细节上因情况而定的多态性,这种多态性是在编译的时候就确定了的,称为静态多态性;一种是动态多态性,是子类继承父类时,子类中函数覆盖父类中同名函数,这样在实际调用该函数时需要根据实例的类型(是父类还是子类)确定使用哪个函数,从而产生的多态性。动态多态性主要是通过虚函数实现的。现列举以下几种情况说明动态多态(假设People
为父类,Student
为子类,二者都有introduce
方法):
- 一般继承情况
- 父类对象,实例化为父类,
introduce
函数调用父类版本;子类对象实例化为子类,调用子类版本。 - 父类对象,实例化为子类。因为实例为父类的对象,所以
introduce
使用父类的版本。 - 父类的指针,指向子类对象。
inroduce
调用父类的版本,因为该实例为父类对象。
综上,一般继承不存在这种动态多态性。以下为示例代码:
//test.h
#ifndef TEST_H
#define TEST_H
#include <iostream>
#include <string>
using namespace std;
class People {
public:
int gender;
string name;
People(string name_, int gender_) :name(name_), gender(gender_) {}
void introduce();
};
class Student :public People {
public:
int id;
int grade;
Student(string name, int gender, int id, int grade) :People::People(name, gender), id(id), grade(grade) {}
void introduce();
};
#endif
//test.cpp
#include "stdafx.h"
#include "test.h"
void People::introduce()
{
cout << "My name is " << name << ", nice to meet you!" << endl;
}
void Student::introduce()
{
cout << "Hello, I'm " << this->name << ". I'm in grade " << this->grade << ", my student number is " << this->id << endl;
}
//main函数
#include "stdafx.h"
#include <iostream>
#include <string>
#include "test.h"
using namespace std;
int main()
{
People p("John", 1);
p.introduce();
Student s("Shelly", 0, 18205, 12);
s.introduce();
People *p1 = new Student("Jack", 1, 14332, 9);
People p2(Student("Tom", 1, 13378, 11));
p1->introduce();
p2.introduce();
system("pause");
return 0;
}
运行结果:
2. 虚类情况
在(类中)函数前加virtual
关键字即可声明该函数为虚函数,含有虚函数的类为虚类。在虚类中情况要稍有不同。
- 父类对象实例化为父类以及子类对象实例化为子类,依然是调用各自版本的同名函数。
- 父类对象实例化为子类。调用父类版本同名函数。
- 父类指针指向子类对象。调用子类版本函数,所谓的动态多态性就是体现在这里。
示例(在以上示例中在People
的introduce
函数前加virtual
)
结果:
另外: - 子类的对象不能实例化为父类(也没有意义)
- c++默认的继承方式是私有继承,这样的话无法(以上面的方式)把父类对象实例化为子类。私有继承没有意义,所以一定要加
public
继承。
- 纯虚类
当虚函数后加个=0
则变为纯虚函数,即该函数指向空,必须重新定义后才可以使用。含有纯虚函数的类为纯虚类,纯虚类不可实例化。