/* (程序頭部註釋開始)
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生
* All rights reserved.* 文件名稱: 000
* 作 者: 楊繼宇
* 完成日期: 2012年05 月16 日
* 版 本 號: 0000
* 對任務及求解方法的描述部分
* 輸入描述:
* 問題描述:
* 程序輸出:
* 程序頭部的註釋結束
*/
#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();
system("pause");
return 0;
}