C++多态的实现条件
2024-08-26 14:27:48
#include <iostream>
class Person{
public:
virtual void say(){
std::cout<<"person"<<std::endl;
}
};
class Student:public Person{
public:
void say(){
std::cout<<"student"<<std::endl;
}
};
int main(){
Person per;
Student stud;
per.say();
stud.say();
//传递对象
per = stud;
per.say();
//指针
Person *pPtr = &stud;
pPtr->say();
//引用
Person &pRef = stud;
pRef.say();
}
1. 基类中用virtual关键字表明方法
2. 采用父类的引用和指针调用多态方法,父类对象本身不能实现多态
最新文章
- ZendStudio13 PHP调试环境快速配置
- Win10 UWP开发中的重复性静态UI绘制小技巧 1
- 分布式日志1 用c#的队列写日志
- java 关键字
- 【原创】Mac上编译Hadoop1.0.3出现的一些问题
- CentOS 6.4 64位 搭建MySQL-Cluster 7.3.8 集群
- CListCtrl 的应用
- HDU 1071 - The area
- poj3094---对字符串的处理
- hdu 2824 The Euler function(欧拉函数)
- (二)、Struts第二天
- Halloc内存分配器
- Go 延迟函数 defer 详解
- request 获取body内容
- Cocos Creator 动态改变sprite图片
- json和jquery中的ajax
- mysql查询时间段内的数据
- CSS规范 - 典型错误--(来自网易)
- JS获取当前项目名
- 简述Dubbo
热门文章
- html中文件类型的accept属性有哪些
- 12 nginx URL 重写 ecshop案例
- JavaWeb学习总结第四篇--Servlet开发
- ASP.NET动态网站制作(8)-- JS(3)
- Grails 简要
- HDU 1452 Happy 2004(唯一分解定理)
- 再看GS接包过程
- EasyPusher华为手机直播推流硬编码[OMX.IMG.TOPAZ.Encoder] failed to set input port definition parameters.
- 前端mvc组合框架
- python使用记录