您的位置:首页 > 其它

交通工具 虚函数的应用

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: