第13周阅读程序——交通工具(1)
2016-05-25 17:21
274 查看
/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称 : *作 者 : 刘云 *完成日期 : 2016年5月25号 *版 本 号 : v6.0 * *问题描述 : 阅读项目(交通工具) *输入描述 : 无 *程序输出 : */ #include<iostream> using namespace std; class Vehicle //交通工具 { public: void run() const { cout<<" run a vehicle. "<<endl; } }; class Car:public Vehicle //汽车 { public: void run() const { cout<<" run a car. "<<endl; } }; class Airplane:public Vehicle //飞机 { public: void run() const { cout<<" run a airplane. "<<endl; } }; int main() { cout<<"(a) 直接用对象访问成员函数:"<<endl; Vehicle v; v.run(); Car car; Airplane airplane; car.run(); airplane.run(); cout<<"(b)用指向基类的指针访问成员函数:"<<endl; Vehicle *vp; vp=&car; vp->run(); vp=&airplane; vp->run(); return 0; }
运行结果:
问题回答:
当基类指针指向派生类时,用指针调用同名函数执行的是——基类成员,
原因:
因为指针为基类指针,所以调用时调用的是基类的成员函数。
相关文章推荐
- CS231n 卷积神经网络与计算机视觉 3 最优化与随机梯度下降
- 最大m子段和模板
- 第十三周实践项目4-立体类族共有的抽象类
- 理解Cookie和Session机制
- android 基础 线程池
- 并查集
- Asp.net MVC 之异常处理
- ASP.NET Core 1.0: Deploy to IIS
- zookeeper基本讲解(Java版,真心不错)
- C# Serializable
- 如何知道TSQL语句已经运行了多久
- MFC显示控制台方法
- FIRST集、FOLLOW集和SELECT集
- javamail发送邮件的简单实例
- T-MVP:泛型深度解耦下的MVP大瘦身
- 大数类模板
- Partition List
- ACM头文件
- 0527 python 基础01
- Android中关于JNI 的学习(二)对于JNI方法名,数据类型和方法签名的一些认识