2013.5.30.2~交通工具类2,将Vehicle类的定义修改为虚函数。。。

来源:互联网 发布:js遍历数组对象的属性 编辑:程序博客网 时间:2024/05/16 09:08
/** Copyright (c) 2012, 烟台大学计算机学院* All rights reserved.* 作    者:王筱菀* 完成日期:2013年5月30日* 版 本 号:v1.0* 输入描述:* 问题描述:阅读修改和运行关于交通工具类的程序* 程序输出:* 问题分析:* 算法设计:略*/#include <iostream>using namespace std;class Vehicle  //交通工具{    public:virtual void run() const{       cout << "run a vehicle. "<<endl;      } //(2) run()为虚函数};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;}

原创粉丝点击