多态分为两类
- 静态多态:函数重载和运算符重载属于静态多社复用函数名
- 动态多态:派生类和虚函数实现运行时多态
静态多态和动态多态区别:
- 静态多态的函数地址早绑定·编译阶段确定函数地址
- 动态多态的函故地址晚绑定·运行阶段确定函教地址下面通过案例进行讲解多态
地址早绑定:
#include<iostream>
#include<string>
using namespace std;
class A
{
public:
void f()
{
cout << "A" << endl;
}
};
class B : public A
{
public:
void f()
{
cout << "B" << endl;
}
};
//地址早绑定,在编译阶段就确定了函数地址
//如果想执行B类的函数就要地址晚绑定
void t(A &p)//A &p=p1
//C++中允许父类和子类间的类型转换,不用强制类型转换
{
p.f();
}
void t1()
{
B p1;
t(p1);
}
int main()
{
t1();
system("pause");
return 0;
}
地址晚绑定:
#include<iostream>
#include<string>
using namespace std;
class A
{
public:
//虚函数,使得地址晚绑定
virtual void f()
{
cout << "A" << endl;
}
};
class B : public A
{
public:
void f()
{
cout << "B" << endl;
}
};
class C :public A
{
public:
void f()
{
cout << "C" << endl;
}
};
//地址早绑定,在编译阶段就确定了函数地址
//如果想执行B类的函数就要地址晚绑定
void t(A &p)//A &p=p1
//C++中允许父类和子类间的类型转换,不用强制类型转换
{
p.f();
}
void t1()
{
B p1;
t(p1);
C p2;
t(p2);
}
int main()
{
t1();
system("pause");
return 0;
}
动态多态满足条件:
1、有继承关系
2、子类重写父类的虚函数
动态多态使用:
父类的指针或者引用指向子类对象
#include<iostream>
#include<string>
using namespace std;
class A
{
public:
//虚函数,使得地址晚绑定
virtual void f()
{
cout << "A" << endl;
}
};
void t2()
{
cout << "sizeof(A)=" << sizeof(A) << endl;
}
int main()
{
t2();
system("pause");
return 0;
}
#include<iostream>
#include<string>
using namespace std;
class A
{
public:
void f()
{
cout << "A" << endl;
}
};
void t2()
{
cout << "sizeof(A)=" << sizeof(A) << endl;
}
int main()
{
t2();
system("pause");
return 0;
}
空类的空对象大小为1,
大小变为4时,是类里面有了一个指针。
vfptr:虚函数(表)指针。
v:virtual
f:function
ptr:pointer
虚函数指针指向虚函数表(vftable)
v:virtual
f:function
表内记录一个虚函数的地址:
&A::f
当子类重写父类的虚函数
子类中的虚函数表内部会替换成子类的虚函数地址
即:&B::f
父类中仍为:&A::f
当父类的指针或者引用指向子类对象时候,发生多态
A &a=b;------->(B b)
a.f();在调用f()函数时,由于指向b所以会从B中找这个虚函数
如果指向a,就会从A类中找这个虚函数。