博客
关于我
20170812_继承与多态测试实例
阅读量:84 次
发布时间:2019-02-26

本文共 1123 字,大约阅读时间需要 3 分钟。

20170812_继承与多态测试实例

/*多态测试*/#include
#include
#include
using namespace std;//基类Baseclass base{public: void fun() { cout<<"I am base::fun()!"<
fun(); //base p_baseobject2->test(); //base class child childobject2; class child *p_childobject2=&childobject2; //派生类指针指向派生类对象: p_childobject2->fun(); //child p_childobject2->test(); //child cout<
fun(); //base(非虚函数,基类指针指向的是派生类中的基类部分!) p_baseobject3->test(); //child(虚函数,这就是:多态性!!!) cout<
基类指针(编译器默认是支持的,也即是可以隐式转换) //上行转换的效果:类似于多态。。。。。。 class child childobject4; class child *p_childobject4=&childobject4; class base *p_baseobject4=static_cast
(p_childobject4); p_baseobject4->fun(); //base:因为是非虚函数,根据指针类型来调用,也就是根据指针所属的类的类型来调用。 p_baseobject4->test(); //child:因为是虚函数,根据指针所指向的对象来调用,调用该对象的函数。 cout<
派生类指针(编译器不支持,需要显示强制类型转换) //下行转换的效果:跟上行转换反过来,是什么鬼?。。。。。。 class child childobject5; class base *p_baseobject5=&childobject5; class child *p_childobject5=static_cast
(p_baseobject5); p_childobject5->fun(); //child:因为是非虚函数,所以根据指针类型来调用,也就是根据指针所属的类的类型来调用。 p_childobject5->test(); //child:因为是虚函数,所以根据指针所指向的对象来调用,调用该对象的函数。 cout<

转载地址:http://pzck.baihongyu.com/

你可能感兴趣的文章
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>