C++多态

多态就是一个事物再不同场景时的多种形态,编程语言中就是完成某个行为时,当不同对象去完成会产生不同的效果。
多态的定义及实现

  • 调用函数的对象必须为指针或引用
  • 被调用函数必须是虚函数,且完成了虚函数的重写

虚函数定义:
         定义类的成员函数时,函数前加virtual关键字。
虚函数的重写:
        派生类中存在和基类完全相同(函数名,参数,返回值)的虚函数,称为子类重写了父类的虚函数。
         特例:协变      重写的虚函数的返回值可以不同,但必须为基类或派生类的引用/指针。

#include<iostream>

using namespace std;

//协变
class A{
public:
	virtual A* fun()
	{
		cout<<"A::fun()"<<endl;
	 	return this;
	}
};
class B : public A{
public:
	virtual B* fun()
	{ 
		cout<<"B::fun()"<<endl;	
		return this;
	}
};

int main()
{
	A a;
	a.fun();
	B b;
	b.fun();
}

协变运行结果

重写虚函数的规范行为:重写的函数,基类和派生类都加上关键字virtual.

析构函数的重写:
析构函数最好写成虚函数。避免父类指针指向子类对象时,析构时调用父类的析构函数(不构成多态)。
析构函数是一个特殊的函数,在编译时,编译器统一将析构函数名称处理为destructor。

//虚函数的重写
#include<iostream>

using namespace std;

class A{
public:
	~A()
	{
		cout<<"~A()"<<endl;
	}
};
class B : public A{
public:
	 ~B()
	{
		cout<<"~B()"<<endl;
	}
};

int main()
{
	A* a = new B;
	delete a;
}

将构造函数定义为虚函数重写,重载,重定义对比:
在这里插入图片描述抽象类
包含纯虚函数的类称为抽象类。
纯虚函数的定义: 虚函数后加 =0即为纯虚函数。
抽象类无法实例化

C++11中的override,final:
final修饰的基类虚函数不能被重写。
override修饰派生类的虚函数,强制完成重写。

多态的实现原理

#include<iostream>

using namespace std;

class A{
public:
	virtual A* fun()
	{
		cout << "A::fun()" << endl;
		return this;
	}
	virtual ~A()
	{
		cout << "~A()" << endl;
	}
	int _a;
};
class B : public A{
public:
	virtual B* fun()
	{
		cout << "B::fun()" << endl;
		return this;
	}
	virtual ~B()
	{
		cout << "~B()" << endl;
	}
	int _b;
};

int main()
{
	A a;
	B b;
	system("pause");
	return 0;
}

虚函数表:由图中可见,a中不仅含有成员变量_a还有一个 _vfptr指针,_vfptr(virtual function pointer)指针称为虚函数表指针,指向虚函数表,虚函数表中存放着对应类的虚函数指针。
观察B类型的b中的虚函数,可见B中的fun对A中的fun完成了重写。B的虚函数表中的fun代表的是
B::fun(),而在a中的fun为A::fun()。
多态的实现依靠的是派生类对基类中的虚函数的重写,所实现的。
所以对类中虚函数的调用只与 引用或指针 指向的对象本身有关。由此解释了析构函数需要写成虚函数的原因,图中也可看出析构函数的函数名称相同

虚函数指针存在对象的头部为对象的第一个成员。

多继承中的虚函数表
单继承中的虚函数表为单一虚函数表,这里就不赘述。派生类中定义的虚函数在虚函数表的后添加。

//单继承
#include<iostream>

using namespace  std;
typedef void(*VFPTR)();		//定义函数指针
 

class Base{
public:
    virtual void fun1(){cout<<"This is Base::fun1"<<endl;};
    virtual void fun2(){cout<<"This is Base::fun2"<<endl;};
private:
    int a;
};

class Drive: public Base{
public:
    virtual void fun1(){cout<<"This is Drive::fun1"<<endl;};
    virtual void fun3(){cout<<"This is Drive::fun3"<<endl;};
    virtual void fun4(){cout<<"This is Drive::fun4"<<endl;};
private:
    int b;
};

void Print(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; 
    Drive d;

    VFPTR* vTableb = (VFPTR*)(*(int *)(&b));            //虚函数表存在对象的头部
    Print(vTableb);

    VFPTR* vTabled = (VFPTR*)(*(int *)(&d));
    Print(vTabled);
    return 0;
}

打印结果多继承中的虚函数表
vs中的运行结果
gcc中和此不同

#include<iostream>

using namespace  std;
typedef void(*VFPTR)();

class Base_first{
public:
	virtual void fun1(){ cout << "This is Base_first::fun1" << endl; }
	virtual void fun2(){ cout << "This is Base_first::fun2" << endl; }
private:
	int _first;
};
class Base_second{
public:
	virtual void fun1(){ cout << "This is Base_second::fun3" << endl; }
	virtual void fun2(){ cout << "This is Base_second::fun4" << endl; }
private:
	int _second;
};
class Drive : public Base_first, public Base_second{
public:
	virtual void fun1(){ cout << "This is Drive::fun1" << endl; }
	virtual void fun3(){ cout << "This is Drive::fun3" << endl; }
};

void Print(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()
{
	Drive d;

	VFPTR* vTabled1 = (VFPTR*)(*(int *)(&d));
	Print(vTabled1);

	VFPTR* vTable2 = (VFPTR*)(*(int*)((char*)&d + sizeof(Base_first)));
	Print(vTable2);
	
	return 0;
}

![vs下运行结果](https://img-blog.csdnimg.cn/20190903212903689.png?x-oss-process=image/watermark,type_ZmFuZ3p

  • 多继承中派生类未重写的虚函数放在第一个直接父类的虚函数表中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值