刚学习的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 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
悬赏问题
- ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
- ¥15 找一个网络防御专家,外包的
- ¥100 能不能让两张不同的图片md5值一样,(有尝)
- ¥15 informer代码训练自己的数据集,改参数怎么改
- ¥15 请看一下,学校实验要求,我需要具体代码
- ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
- ¥20 MATLAB绘制两隐函数曲面的交线
- ¥15 求TYPCE母转母转接头24PIN线路板图
- ¥100 国外网络搭建,有偿交流
- ¥15 高价求中通快递查询接口