刚学习的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没有什么区别,求解!!!
继承与派生和虚基类函数的
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-