我是靠谱客的博主 隐形手链,这篇文章主要介绍第十三周实践项目课后————交通工具类(2),现在分享给大家,希望可以做个参考。

问题及代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*copyright(c)2016.烟台大学计算机学院 * All rights reserved, * 文件名称:text.Cpp * 作者:吴敬超 * 完成日期:2016年5月23日 * 版本号:codeblock * * 问题描述: * 输入描述: * 程序输出: 输出结果 */ #include<iostream> using namespace std; class Vehicle { public: virtual 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; }

运行结果:

课后问答:

当vehicle定义为虚函数是调用的是派生类中的成员函数,C++多态性是通过虚函数来实现的,多态性是虚函数允许子类重新定义成员函数,而子类重新定义父类的做法称为覆盖。

最后

以上就是隐形手链最近收集整理的关于第十三周实践项目课后————交通工具类(2)的全部内容,更多相关第十三周实践项目课后————交通工具类(2)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(87)

评论列表共有 0 条评论

立即
投稿
返回
顶部