继承与派生和虚基类函数的

刚学习的c++中的虚基类,然后发现并不懂求大神
#include
using namespace std;
class T0
{
protected:
int x;
public:
T0(int a = 0){ x = a; cout << "爷爷类\n"; }
virtual void show(){ cout << "this is grandpa!\n"; }
~T0(){ cout << "grandpa\n"; }
};
class T1 :virtual public T0
{
protected:
int y;
public:
T1(int a = 0, int b = 0) :T0(a){ y = b; cout << "father class\n"; }
virtual void show(){ cout << "this is father!\n"; }
~T1(){ cout << "father\n"; }
};
class T12 :virtual public T0
{
protected:
int y1;
public:
T12(int a = 0, int b = 0) :T0(a){ y1 = b; cout << "father class\n"; }
virtual void show(){ cout << "this is father!\n"; }
~T12(){ cout << "father\n"; }
};
class T2 :public T1, public T12
{
protected:
int z;
public:
T2(int a = 0, int b = 0, int c = 0) :T0(a), T1(a, b){ z = c; cout << "son class\n"; }
~T2(){ cout << "son\n"; }
virtual void show(){ cout << "this is son!\n"; }
};
int main()
{
T0 t01(3);
T1 t11(4, 3);
T2 t21(1, 2, 3);
t01.show();
t11.show();
t21.show();
T0 *T0p = &t01;
T0p->show();
*T0p = t11;
T0p->show();
*T0p = t21;
T0p->show();
system("pause");
return 0;
}
中最后三个指针指向的函数,加不加virtual没有什么区别,求解!!!

加virtual表示是虚函数,当父类指针指向子类时能调用子类对应 的方法。
不加的话,父类调用的就是自己的方法。就没有动态多态这一说了

你后两个基类指针使用不对,改后就能看到不同了。

    T0 *T0p = &t01;
    T0p->show();
    T0p = &t11;  //用地址
    T0p->show();
    T0p = &t21;//用地址
    T0p->show();