博客
关于我
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/

你可能感兴趣的文章
Nginx 学习(一):Nginx 下载和启动
查看>>
nginx 常用指令配置总结
查看>>
Nginx 常用配置清单
查看>>
nginx 常用配置记录
查看>>
nginx 开启ssl模块 [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx
查看>>
Nginx 我们必须知道的那些事
查看>>
Nginx 的 proxy_pass 使用简介
查看>>
Nginx 的配置文件中的 keepalive 介绍
查看>>
Nginx 结合 consul 实现动态负载均衡
查看>>
Nginx 负载均衡与权重配置解析
查看>>
Nginx 负载均衡详解
查看>>
nginx 配置 单页面应用的解决方案
查看>>
nginx 配置https(一)—— 自签名证书
查看>>
nginx 配置~~~本身就是一个静态资源的服务器
查看>>
Nginx 配置清单(一篇够用)
查看>>
Nginx 配置解析:从基础到高级应用指南
查看>>
nginx+php的搭建
查看>>
nginx+tomcat+memcached
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>