交通工具 虚函数的应用
2016-06-18 12:18
211 查看
/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作 者:刘亚
*完成日期:2016年 6月 7日
*版 本 号:1.0
*
*问题描述:交通工具 虚函数的应用
*输入描述:
*程序输出:
*/
#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;
}
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作 者:刘亚
*完成日期:2016年 6月 7日
*版 本 号:1.0
*
*问题描述:交通工具 虚函数的应用
*输入描述:
*程序输出:
*/
#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;
}
相关文章推荐
- 第十三周项目1(2)
- jqPlot jQuery绘图插件的使用
- 字典树(Trie树)
- 黑洞数
- eclipse中解决65536
- RxJava
- 215. Kth Largest Element in an Array
- faster_rcnn c++版本的 caffe 封装,动态库(2)
- 3ds max 渲染清晰面片的边缘
- JQuery_Validation_plugin
- 第一个博客 属于自己的
- C程序编译过程浅析
- scikit-learn : 线性回归
- MongoDB的性能监控
- faster_rcnn c++版本的 caffe 封装(1)
- 第十三周项目1(1)
- memcache对hash一致性算法和取模算法详解
- 1018. 锤子剪刀布 (20)
- 论文阅读笔记:You Only Look Once: Unified, Real-Time Object Detection
- openwrt启动脚本分析