虚函数
静态多态(编译时期,早绑定)
动态多态(运行时期,晚绑定)
virtual 虚函数(多态)
虚析构函数
virtual
保证内存不泄露 父类在delete的时候,子类new的内存也能释放
virtual不能修饰 全局函数
静态函数
内联函数
构造函数
虚函数的原理
函数指针:函数入口地址
虚函数表指针
虚函数表
函数的覆盖与隐藏
虚析构函数的原理
执行完子类的析构函数就会执行父类的析构函数
虚函数表指针的存在
对象的大小:成员变量的大小,空的类用1byte标识自己的存在
对象的地址:内存的手地址
对象成员的地址:
虚函数表指针:在有虚函数的时候,第一块内存的地址 4byte
每个类只有一份虚函数表,所有该类的对象共用同一张虚函数表
纯虚函数
virtual double calArea() = 0; // 虚函数表中的指针为0
抽象类
含有纯虚函数的类叫做抽象类
接口类
代表一种能力
接口类中仅有纯虚函数,不能含有其它函数,也不含有数据成员
#ifndef FLYABLE_H
#define FLYABLE_H
class Flyable {
public:
virtual void takeoff()=0;
virtual void land() = 0;
};
endif // FLYABLE_H
RTTI
运行时识别 和java中的instanceof差不多
typeid:
typeid返回一个type_info对象的引用
如果想通过基类的指针获得派生类的数据类型,基类必须带有虚函数
只能获得对象的实际类型
dynamic_cast:
只能用于指针和引用的转换
要转换的类型中必须包含虚函数
转换成功返回子类的地址,失败返回NULL
void doSomething(Flyable *obj) {// Flyable一直有takeoff和land方法的接口
cout << typeid(*obj).name() << endl;
obj->takeoff();
if (typeid(*obj) == typeid(Bird)) { // 需要#include<typeinfo>
Bird *bird = dynamic_cast<Bird *>(obj);// 继承Flyable,自带foraging()方法
bird->foraging();
}
if (typeid(*obj) == typeid(Plane)) {// 继承Flyable,自带carry()方法
Plane *plane = dynamic_cast<Plane *>(obj);
plane->carry();
}
obj->land();
}
异常处理
对有可能发生异常的地方做出预见性的安排
try...catch... throw
尝试 捕获 抛出
catch(Exception &e)
catch(int)
catch(...)
Exception.h
#ifndef EXCEPTION_H
#define EXCEPTION_H
class Exception {
public:
virtual ~Exception() {}
virtual void printException();
};
#endif // EXCEPTION_H
Exception.cpp
#include "Exception.h"
#include <iostream>
using namespace std;
void Exception::printException() {
cout << "Exception::printException" << endl;
}
IndexException.h
#ifndef INDEXEXCEPTION_H
#define INDEXEXCEPTION_H
#include "Exception.h"
class IndexException : public Exception{
public:
virtual void printException();
};
#endif // INDEXEXCEPTION_H
IndexException.cpp
#include "IndexException.h"
#include <iostream>
using namespace std;
void IndexException::printException() {
cout << "提示:下标越界. " << endl;
}
main.cpp
#include <iostream>
#include "IndexException.h"
using namespace std;
void test() {
throw IndexException();
}
int main() {
try {
test();
} catch(IndexException &e) {
e.printException();
} catch(Exception &e) {
e.printException();
}
return 0;
}