📄 virtualdemo.cpp
字号:
// VirtualDemo.cpp : 虚函数演示
//
#include <stdio.h>
class ShapeVirtual
{
public:
int x;
virtual void ShowName()
{
printf("\nShapeVirtual对象的ShowName函数被调用\n");
printf("x=%d\n", x);
}
virtual void ShowName(int a)
{
printf("\nShapeVirtual对象的ShowName(int a)函数被调用\n");
printf("x=%d\n", x);
}
void func1() {}
virtual void func2() {}
};
//类PointVirtual继承于类ShapeVirtual
//
class PointVirtual : public ShapeVirtual
{
public:
//int x;
using ShapeVirtual::ShowName;
void ShowName()
{
printf("\nPointVirtual对象的ShowName函数被调用\n");
printf("x=%d\n", x);
}
};
int main(int argc, char* argv[])
{
//创建ShapeHide对象
ShapeVirtual *s = new ShapeVirtual();
s->x = 1;
s->ShowName();
s->ShowName(1);
//创建PointHide对象
PointVirtual *p = new PointVirtual();
p->x = 2;
p->ShowName();
p->ShowName(1);
//s指向ShapeVirtual对象
delete s;
//让ShapeVirtual对象指针,指向PointVirtual对象
s = p;
s->ShowName();
s->ShowName(1);
delete p;
s = NULL;
p = NULL;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -