C++中的多态
1、多态的概念
多态指的是在不同继承关系的类对象中,去调用同一函数产生的不同行为
构成多态的条件:
1、必须通过基类指针或引用调用虚函数
2、派生类必须对基类的虚函数进行重写
#include <iostream>
using namespace std;
class Person
{
public:
virtual void doSomething()
{
cout << "Person" << endl;
}
};
class Student : public Person
{
virtual void doSomething()
{
cout << "Student" << endl;
}
};
void Func(Person& people)
{
people.doSomething();
}
int main()
{
Person zhangsan;
Func(zhangsan);
Student lisi;
Func(lisi);
system("pause");
return 0;
}
2、多态的实现
2.1、虚函数
被virtual修饰的成员函数称为虚函数
2.2、虚函数的重写
虚函数的重写(覆盖):派生类只有一个跟着基类完全相同的虚函数(即派生类虚函数与基类虚函数的返回值类型、函数名字、参数列表 完全相同)称为派生类的虚函数重写了基类的虚函数
虚函数的重写,派生类可以不加virtual关键字,虽然也可以构成重写关系,但是继承后基类的虚函数被继承下来了在派生类依旧保持虚函数属性,但是写法不规范
#include <iostream>
using namespace std;
class Person
{
public:
virtual void doSomething()
{
cout << "Person" << endl;
}
};
class Student : public Person
{
public:
virtual void doSomething()
{
cout << "Student" << endl;
}
};
void Func(Person& people)
{
people.doSomething();
}
int main()
{
Person per;
Func(per);
Student stu;
Func(stu);
system("pause");
return 0;
}
2.3、虚函数重写的特例
2.3.1、协变
派生类重写基类虚函数的时候,与基类虚函数返回值类型不同,即基类虚函数返回基类对象的指针或者引用,派生类虚函数返回派生类对象的指针或引用时,称为协变
==假如B继承自A,D继承自C,那么A中虚函数返回了C类指针,B中虚函数返回 了D类指针,这种情况也构成重写
==
#include <iostream>
using namespace std;
//协变(基类与派生类虚函数返回值类型不同)
//派生类重写基类虚函数的时候,与基类虚函数返回值类型不同,即基类虚函数返回基类对象的指针或者引用,派生类虚函数返回派生类对象的指针或引用时,称为协变
class A
{
};
class B : public A
{
};
class Person
{
public:
virtual A* f()
{
return new A;
}
};
class Student : public Person
{
public:
virtual B* f()
{
return new B;
}
};
int main()
{
system("pause");
return 0;
}
2.3.2、虚析构函数
若是基类的析构函数为虚函数,此时派生类析构函数只要定义,无论是否加virtual关键字,都与基类的虚构函数构成重写,虽然基类与派生类析构函数名字不同,虽然函数名不同,但是这里可以理解为编译后析构函数名称统一处理为destructor
虚析构函数用来解决派生类指针(对象)转化为基类指针(对象)进行析构的问题.
#include <iostream>
using namespace std;
//析构函数的重写
//若是基类的析构函数为虚函数,此时派生类析构函数只要定义,无论是否加virtual关键字,都与基类的虚构函数构成重写,虽然基类与派生类析构函数名字不同,虽然函数名不同,但是这里可以理解为编译后析构函数名称统一处理为destructor
class Person
{
public:
virtual ~Person()
{
cout << "~Person()" << endl;
}
};
class Student : public Person
{
public:
virtual ~Student()
{
cout << "~Student()" << endl;
}
};
//只有派生类的析构函数重写了基类的析构函数,此时delete对象调用析构函数才可以构成多态,才能保证指针指向的对象正确调用析构函数
int main()
{
Person* per = new Person;
Student* stu = new Student;
delete per;
delete stu;
system("pause");
return 0;
}
3、抽象类
抽象类:在虚函数后面写上 “= 0”,则这个函数为纯虚函数,包含纯虚函数的类叫做抽象类
抽象类不能实例化出对象,只有重写后的纯虚函数才能实例化出对象
纯虚函数规范了派生类必须重写,纯虚函数更体现出了接口继承
抽象类的特点:
1、包含纯虚函数
2、不能定义对象
#include <iostream>
using namespace std;
class Car
{
public:
virtual void Drive() = 0;
};
class Benz : public Car
{
virtual void Drive()
{
cout << "Benz" << endl;
}
};
class BMW : public Car
{
virtual void Drive()
{
cout << "BMW" << endl;
}
};
int main()
{
Car* pBenz = new Benz;
pBenz->Drive();
Car* pBMW = new BMW;
pBMW->Drive();
system("pause");
return 0;
}
4、C++11中的final和override关键字
4.1、final关键字
不能被继承的类或者不能被重写的虚函数
#include <iostream>
using namespace std;
//final修饰的虚函数不能再被继承
class Car
{
public:
virtual void Drive() final
{
cout << "Car" << endl;
}
};
class Benz : public Car
{
public:
virtual void Drive()
{
cout << "Benz" << endl;
}
};
int main()
{
system("pause");
return 0;
}
4.2、override关键字
声明派生类的某个函数必须重写基类的某个虚函数(派生类)
#include <iostream>
using namespace std;
//override修饰的虚函数用来检查派生类虚函数是否重写了基类的某个虚函数,若是没有重写,编译报错
class Car
{
public:
virtual void Drive()
{
cout << "Car" << endl;
}
};
class Benz : public Car
{
public:
virtual void Drive() override
{
cout << "Benz" << endl;
}
};
int main()
{
system("pause");
return 0;
}
5、多态原理
5.1、虚函数表
1、派生类中也有序表指针,一部分是存虚函数,一部分存成员
2、基类和派生类的序表不一样,虚函数的重写也叫覆盖
3、虚函数的本质是一个存虚函数指针的指针数组,这个数组最后放的是nullptr
4、序表存的是虚函数指针,序表存在代码段
派生类序表生成总结:
1、基类序表内容拷贝一份到派生类序表中
2、若是派生类重写了基类虚函数,则派生类自己的虚函数覆盖序表中基类的虚函数
3、派生类自己新增的虚函数按其在派生类中的声明次序增加到派生类序表的最后
#include <iostream>
using namespace std;
class Base
{
public:
virtual void Func1()
{
cout << "Base::Func1()" << endl;
}
virtual void Func2()
{
cout << "Base::Func2()" << endl;
}
void Func3()
{
cout << "Base::Func3()" << endl;
}
public:
int m_b = 1;
};
class Derive : public Base
{
public:
virtual void Func1()
{
cout << "Derive::Func1()" << endl;
}
private:
int m_d = 2;
};
int main()
{
Base b;
Derive d;
system("pause");
return 0;
}
5.2、多态原理
不同对象完成同一行为时候会展现出多态
要实现多态,就要实现虚函数的覆盖和对象的指针或引用调用虚函数
#include <iostream>
using namespace std;
class Person
{
public:
virtual void doSomething()
{
cout << "Person" << endl;
}
};
class Student : public Person
{
public:
virtual void doSomething()
{
cout << "Student" << endl;
}
};
void Func(Person& people)
{
people.doSomething();
}
int main()
{
Person per;
Func(per);
Student stu;
Func(stu);
system("pause");
return 0;
}
5.3、动态绑定和静态绑定
静态绑定:在程序编译期间确定程序的行为,也称为静态多态(函数重载属于静态绑定)
动态绑定:在程序运行期间,根据拿到的类型来确定程序的具体行为,调用具体的函数
6、单继承中的序表
#include <iostream>
using namespace std;
class Base
{
public:
virtual void func1()
{
cout << "Base::func1()" << endl;
}
virtual void func2()
{
cout << "Base::func2()" << endl;
}
private:
int m_a;
};
class Derive : public Base
{
public:
virtual void func1()
{
cout << "Derive::func1()" << endl;
}
virtual void func3()
{
cout << "Derive::func3()" << endl;
}
virtual void func4()
{
cout << "Derive::func4()" << endl;
}
private:
int m_b;
};
typedef void(*VFPTR) ();
void PrintVTable(VFPTR vTable[])
{
cout << "序表地址" << vTable << endl;
for (int i = 0; vTable[i] != nullptr; i++)
{
printf("第%d个虚函数地址:0X%x,->", i, vTable[i]);
VFPTR f = vTable[i];
f();
}
cout << endl;
}
int main()
{
Base b;
Derive d;
//思路:取出b,d对象的前四个字节,就是序表的指针
//1、先取出b的地址强转成int*的指针
//2、解引用取值,就取到b对象的4个字节的值
//3、再强转成VFPTR*,因为序表就是存VFPTR(虚函数指针)类型的数组
//4、序表指针传递给PrintVTable打印序表
VFPTR* vTableb = (VFPTR*)(*(int*)&b);
PrintVTable(vTableb);
VFPTR* vTabled = (VFPTR*)(*(int*)&d);
PrintVTable(vTabled);
system("pause");
return 0;
}
7、多继承中的序表
多继承派生类的未重写的虚函数放在第一个继承基类部分的虚函数表中
#include <iostream>
using namespace std;
//多继承派生类的未重写的虚函数放在第一个继承基类部分的虚函数表中
class Base1
{
public:
virtual void func1()
{
cout << "Base1::func1()" << endl;
}
virtual void func2()
{
cout << "base1::func2()" << endl;
}
private:
int m_b1;
};
class Base2
{
public:
virtual void func1()
{
cout << "Base2::func1()" << endl;
}
virtual void func2()
{
cout << "Base2::func2()" << endl;
}
private:
int m_b2;
};
class Derive : public Base1, public Base2
{
public:
virtual void func1()
{
cout << "Derive::func1()" << endl;
}
virtual void func3()
{
cout << "Derive::func3()" << endl;
}
private:
int m_d;
};
typedef void(*VFPTR) ();
void PrintVTable(VFPTR vTable[])
{
cout << "序表地址" << vTable << endl;
for (int i = 0; vTable[i] != nullptr; i++)
{
printf("第%d个虚函数地址:0X%x,->", i, vTable[i]);
VFPTR f = vTable[i];
f();
}
cout << endl;
}
int main()
{
Base1 b1;
Base2 b2;
VFPTR* Base1b1 = (VFPTR*)(*(int*)&b1);
PrintVTable(Base1b1);
VFPTR* Base1b2 = (VFPTR*)(*(int*)&b2);
PrintVTable(Base1b2);
Derive d;
VFPTR* Derived1 = (VFPTR*)(*(int*)&d);
PrintVTable(Derived1);
VFPTR* Derived2 = (VFPTR*)(*(int*)((char*)&d+sizeof(Base1)));
PrintVTable(Derived2);
system("pause");
return 0;
}