# 菱形继承（虚函数）->菱形虚拟继承（虚函数）->多态系列问题

## 多态

class A
{
public:
A(int x)//构造函数
{
a = x;
}
virtual void print()//虚函数
{
cout << "A::" << a << endl;
}
private:
int a;
};

class B :public A
{
public:
B(int x, int y)
:A(x)
{
b = y;
}
virtual void print()//重写
{
cout << "B::" << b << endl;
}
private:
int b;
};

void test()
{
A a(10);
B b(15,20);
A *c = &a;
c->print();
c = &b;
c->print();

}

1.构造函数（拷贝构造）

2.内联函数

3.静态成员函数

4.友元函数

5.赋值运算符重载

6.析构函数

## 继承

class A
{
public:
virtual void Funtest1()
{
cout << "A::Funtest1" << endl;
}
virtual void Funtest2()
{
cout << "A::Funtest2" << endl;
}
virtual void Funtest3()
{
cout << "A::Funtest3" << endl;
}
virtual void Funtest4()
{
cout << "A::Funtest4" << endl;
}
};

typedef void(*PVTF)();

void PrintVpt()
{
A a;
PVTF* pVtf = (PVTF*)(*(int*)&a);

while (*pVtf)
{
(*pVtf)();
pVtf++;
}
}
void test()
{
cout << sizeof(A) << endl;

PrintVpt();
}

## 单继承

class A
{
public:
virtual void Funtest1()
{
cout << "A::Funtest1" << endl;
}
virtual void Funtest2()
{
cout << "A::Funtest2" << endl;
}
virtual void Funtest3()
{
cout << "A::Funtest3" << endl;
}
virtual void Funtest4()
{
cout << "A::Funtest4" << endl;
}
};

class B :public A
{

};
typedef void(*PVTF)();//函数指针

void PrintVpt()//打印虚表
{
B b;
PVTF* pVtf = (PVTF*)(*(int*)&b);

while (*pVtf)
{
(*pVtf)();
pVtf++;
}
}
void test()
{
cout << sizeof(A) << endl;
cout << sizeof(B) << endl;

PrintVpt();
}

class A
{
public:
virtual void Funtest1()
{
cout << "A::Funtest1" << endl;
}
virtual void Funtest2()
{
cout << "A::Funtest2" << endl;
}
virtual void Funtest3()
{
cout << "A::Funtest3" << endl;
}
virtual void Funtest4()
{
cout << "A::Funtest4" << endl;
}
};

class B :public A
{
public:
virtual void Funtest2()
{
cout << "B::Funtest2" << endl;
}
virtual void Funtest5()
{
cout << "B::Funtest5" << endl;
}
};
typedef void(*PVTF)();//函数指针

void PrintVpt()//打印虚表
{
B b;
PVTF* pVtf = (PVTF*)(*(int*)&b);

while (*pVtf)
{
(*pVtf)();
pVtf++;
}
}
void test()
{
cout << sizeof(A) << endl;
cout << sizeof(B) << endl;

PrintVpt();
}

## 多继承

class A
{
public:
virtual void Funtest1()
{
cout << "A::Funtest1" << endl;
}
};

class B
{
public:
virtual void Funtest2()
{
cout << "B::Funtest2" << endl;
}
};

class C :public A, public B
{
public:

virtual void Funtest1()
{
cout << "C::Funtest1" << endl;
}
virtual void Funtest2()
{
cout << "c::Funtest2" << endl;
}
virtual void Funtest4()
{
cout << "C::Funtest4" << endl;
}
};

typedef void(*PVTF)();//函数指针

void PrintVpt()//打印虚表
{
C c;
PVTF* pVtf = (PVTF*)(*(int*)&c);

while (*pVtf)
{
(*pVtf)();
pVtf++;
}
A a;
B &b = c;
pVtf = (PVTF*)*(int*)&b;
while (*pVtf)
{
(*pVtf)();
pVtf++;
}
}
void test()
{

cout << sizeof(A) << endl;
cout << sizeof(B) << endl;
cout << sizeof(C) << endl;
PrintVpt();
}

## 菱形继承（钻石继承）

B1的虚表中包含了最初的基类A，以及派生类C的虚函数，B2的虚表中包含了最初的基类A的虚函数。同样的，如果派生类中对基类B1、B2中的虚函数进行了重写，分别替换基类B1、B2虚表中的虚函数，同理B1、B2中对A类的虚函数进行了重写，也会替换。

class A
{
public:
virtual void Funtest1()
{
cout << "A::Funtest1" << endl;
}
};

class B1:public A
{
public:
virtual void Funtest2()
{
cout << "B1::Funtest2" << endl;
}
};

class B2 :public A
{
public:

virtual void Funtest3()
{
cout << "B2::Funtest3" << endl;
}

};

class C :public B1,public B2
{
public:

virtual void Funtest4()
{
cout << "C::Funtest4" << endl;
}
};

typedef void(*PVTF)();//函数指针

void PrintVpt()//打印虚表
{
C c;
PVTF* pVtf = (PVTF*)(*(int*)&c);

while (*pVtf)
{
(*pVtf)();
pVtf++;
}
B2 &b = c;
pVtf = (PVTF*)*(int*)&b;
while (*pVtf)
{
(*pVtf)();
pVtf++;
}
}
void test()
{

cout << sizeof(B1) << endl;
cout << sizeof(B2) << endl;
cout << sizeof(C) << endl;
PrintVpt();
}

## 虚拟继承

class A
{
public:
virtual void Funtest1()
{
cout << "A::Funtest1" << endl;
}
};

class B :virtual public A
{
public:
virtual void Funtest2()
{
cout << "B::Funtest2" << endl;
}
virtual void Funtest1()
{
cout << "B::Funtest1" << endl;
}
};
typedef void(*PVTF)();//函数指针

void PrintVpt()//打印虚表
{
B b;
PVTF* pVtf = (PVTF*)(*(int*)&b);

while (*pVtf)
{
(*pVtf)();
pVtf++;
}
A &a = b;
pVtf = (PVTF*)*((int*)&a);
while (*pVtf)
{
(*pVtf)();
pVtf++;
}
}
void test()
{
cout << sizeof(A) << endl;
cout << sizeof(B) << endl;

PrintVpt();
}

## 菱形虚拟继承

class A
{
public:
virtual void Funtest1()
{
cout << "A::Funtest1" << endl;
}

int _a;
};
class B1 :virtual public A
{
public:
virtual void Funtest2()
{
cout << "B1::Funtest2" << endl;
}

int _b1;
};
class B2 :virtual public A
{
public:
virtual void Funtest3()
{
cout << "B2::Funtest3" << endl;
}
virtual void Funtest1()
{
cout << "B2::Funtest1" << endl;
}

int _b2;
};
class C :public B1, public B2
{
public:
virtual void Funtest3()
{
cout << "C::Funtest3" << endl;
}
virtual void Funtest4()
{
cout << "C::Funtest4" << endl;
}

int _c;
};

typedef void(*PVTF)();

void PrintVft()
{

C c;//创建派生类对象d
c._a = 1;
c._b1 = 2;
c._b2 = 3;
c._c = 4;
PVTF* pVtf= (PVTF*)*(int*)&c;

while (*pVtf)
{
(*pVtf)();
pVtf++;
}

cout << endl << endl;

pVtf = (PVTF*)*((int*)&c + 3);
while (*pVtf)
{
(*pVtf)();
pVtf++;
}

cout << endl << endl;

pVtf = (PVTF*)*((int*)&c + 7);
while (*pVtf)
{
(*pVtf)();
pVtf++;
}
}
void test()
{
cout << sizeof(C) << endl << endl;
PrintVft();

}

04-16 680

04-12 2189
12-28 374
03-18 528
06-19 31
02-22 1366
10-11 1494
05-03 237
05-06 446
07-21 2378
08-02 1080
12-18 2257
02-16 706
05-06 721
10-05 117
02-16 562
01-15 1061