个人主页:仍有未知等待探索-CSDN博客
专题分栏:C++
请多多指教!
目录
一、虚函数表
二、单继承(无虚函数覆盖)
继承关系表:
对于实例:derive d 的虚函数表:
对于实例:base b 的虚函数表:
三、单继承(有虚函数覆盖)
继承关系表:
对于实例:derive d 的虚函数表:
对于实例:base b 的虚函数表:
四、多继承(无虚函数覆盖)
继承关系表:
对于实例:derive d 的虚函数表:
对于实例:base1 b 的虚函数表:
五、多继承(有虚函数覆盖)
继承关系表:
对于实例:derive d 的虚函数表:
对于实例:base1 b 的虚函数表:
一、虚函数表
在这个虚函数表中,主要有一个类的虚函数的地址。当我们用基类指针操作派生类的时候,其指明了实际应该调用的函数。
二、单继承(无虚函数覆盖)
class base {
public :virtual void func1() { cout<<"base::func1" <<endl;}virtual void func2() {cout<<"base::func2" <<endl;}
private :int a;
};
class derive :public base {
public :virtual void func3() {cout<<"derive::func3" <<endl;}virtual void func4() {cout<<"derive::func4" <<endl;}
private :int b;
};
继承关系表:
对于实例:derive d 的虚函数表:
对于实例:base b 的虚函数表:
三、单继承(有虚函数覆盖)
class base {
public :virtual void func1() { cout<<"base::func1" <<endl;}virtual void func2() {cout<<"base::func2" <<endl;}
private :int 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 b;
};
继承关系表:
对于实例:derive d 的虚函数表:
对于实例:base b 的虚函数表:
四、多继承(无虚函数覆盖)
class base1
{
public:virtual void f(){ cout << "base1::f()" << endl; }virtual void g(){ cout << "base1::g()" << endl; }virtual void h(){ cout << "base1::h()" << endl; }
};class base2
{
public:virtual void f(){ cout << "base2::f()" << endl; }virtual void g(){ cout << "base2::g()" << endl; }virtual void h(){ cout << "base2::h()" << endl; }
};class base3
{
public:virtual void f(){ cout << "base3::f()" << endl; }virtual void g(){ cout << "base3::g()" << endl; }virtual void h(){ cout << "base3::h()" << endl; }
};class derive : public base1, public base2, public base3
{
public:virtual void f1(){ cout << "derive::f1()" << endl; }virtual void g1(){ cout << "derive::g1()" << endl; }
};
继承关系表:
对于实例:derive d 的虚函数表:
对于实例:base1 b 的虚函数表:
其他基类同理。
五、多继承(有虚函数覆盖)
class base1
{
public:virtual void f(){ cout << "base1::f()" << endl; }virtual void g(){ cout << "base1::g()" << endl; }virtual void h(){ cout << "base1::h()" << endl; }
};class base2
{
public:virtual void f(){ cout << "base2::f()" << endl; }virtual void g(){ cout << "base2::g()" << endl; }virtual void h(){ cout << "base2::h()" << endl; }
};class base3
{
public:virtual void f(){ cout << "base3::f()" << endl; }virtual void g(){ cout << "base3::g()" << endl; }virtual void h(){ cout << "base3::h()" << endl; }
};class derive : public base1, public base2, public base3
{
public:virtual void f(){ cout << "derive::f()" << endl; }virtual void g1(){ cout << "derive::g1()" << endl; }
};