多态定义
多态分为静态多态和动态多态。静态多态在编译阶段就确定了函数的地址,而动态多态要到运行时才可以确定函数的地址。函数重载和运算符重载属于静态多态。
虚函数、纯虚函数、普通函数
- 虚函数:通过关键字virtual修饰的函数,父类中提供了虚函数的实现,为子类提供默认的函数实现。也就是子类如果不实现虚函数,默认调用父类的虚函数实现,子类可以重写父类的虚函数。
- 纯虚函数:通过关键字virtual修饰的函数,父类不实现纯虚函数,也就是只提供声明,不提供实现,子类必须要实现纯虚函数。如果class中包含了纯虚函数,那么该class被称为抽象类。抽象类不能new出来对象,只有实现了其纯虚函数的子类可以new出来对象。
- 普通函数:普通函数没有多态的概念,是父类为子类提供的强制实现,子类不应该重写父类的普通函数
#include <iostream>
#include <string>
using namespace std;
class Father {
public:
/* 虚函数 */
virtual void func1() {
cout << "is Father func1()" << endl;
}
/* 纯虚函数 */
virtual void func2() = 0;
/* 普通函数 */
void func3() {
cout << "is Father func3()" << endl;
}
};
class Son : public Father {
public:
virtual void func1() {
cout << "is Son func1()" << endl;
}
virtual void func2() {
cout << "is Son func2()" << endl;
}
void func3() {
cout << "is Son func3()" << endl;
}
};
int main(int argc, char const* argv[]) {
Father* f = new Son();
f->func1(); /* 输出 is Son func1(),如果子类没有重写父类的虚函数,就会输出 is Father func2() */
f->func2(); /* 输出 is Son func2() */
f->func3(); /* 输出 is Father func3() */
delete f;
return 0;
}
虚析构、纯虚析构
和虚函数、纯虚函数的的概念类似,如果是虚析构子类可以不重写析构函数,如果是纯虚析构则需要重写,含有纯虚析构的类叫抽象类。虚析构和纯虚析构是用来解决通过父类指针释放子类对象,如果子类中没有堆区的数据,可以不写虚析构或者纯虚析构。
#include <iostream>
#include <string>
using namespace std;
class Father {
public:
/* 纯虚函数 */
virtual void func() = 0;
/* 虚析构,如果父类不是虚析构或者纯虚析构,释放的时候子类的析构函数将无法被调用 */
virtual ~Father() {
cout << "~Father()" << endl;
}
};
class Son : public Father {
public:
virtual void func() {
cout << "value: " << *value << endl;
}
Son(int value) {
this->value = new int(value);
}
~Son() {
cout << "~Son()" << endl;
if (value != NULL) {
cout << "delete value" << endl;
delete value;
value = NULL;
}
}
int* value;
};
int main(int argc, char const* argv[]) {
Father* f = new Son(10);
f->func(); /* 输出:value: 10 */
delete f;
/* 父类不含虚析构的调用顺序:
1. value: 10
2. ~Father()
*/
/* 父类含有虚析构的调用顺序:
1. value: 10
2. ~Son()
3. delete value
4. ~Father()
*/
return 0;
}
示例
#include <iostream>
#include <string>
using namespace std;
/* 多态案例:
1.生物识别有2种:Fingerprint, Face
2.TEE有2种:TRUSTONIC, QSEE
3.组装
*/
/* 生物识别抽象类 */
class BiometricsModule {
public:
virtual void biometrics() = 0;
};
/* TEE抽象类 */
class TEEModule {
public:
virtual void tee() = 0;
};
/* Fingerprint */
class Fingerprint : public BiometricsModule {
public:
virtual void biometrics() {
cout << "Fingerprint biometrics" << endl;
}
};
/* Face */
class Face : public BiometricsModule {
public:
virtual void biometrics() {
cout << "Face biometrics" << endl;
}
};
/* TRUSTONIC tee */
class Trustonic : public TEEModule {
public:
virtual void tee() {
cout << "TRUSTONIC tee" << endl;
}
};
/* QSEE tee */
class Qsee : public TEEModule {
public:
virtual void tee() {
cout << "QSEE tee" << endl;
}
};
/* 手机类 */
class MobilePhone {
public:
MobilePhone(BiometricsModule* bio, TEEModule* ta) {
this->bio = bio;
this->ta = ta;
}
~MobilePhone() {
if (this->bio != NULL) {
delete this->bio;
this->bio = NULL;
}
if (this->ta != NULL) {
delete this->ta;
this->ta = NULL;
}
}
void work() {
this->bio->biometrics();
this->ta->tee();
}
BiometricsModule* bio;
TEEModule* ta;
};
int main(int argc, const char** argv) {
MobilePhone xiaomi(new Fingerprint, new Trustonic);
cout << "xiamo" << endl;
xiaomi.work();
cout << "=========================" << endl;
MobilePhone* vivo = new MobilePhone(new Face, new Qsee);
cout << "vivo" << endl;
vivo->work();
return 0;
}