12 2虛基類騎電單車

2021-07-03 01:24:30 字數 785 閱讀 1887

#include #include#include using namespace std;

enum vehiclestaus ; //車輛狀態:泊車、行進

class vehicle //車輛類

;class bicycle :virtual public vehicle//(1)自行車類的虛基類為車輛類

;class motorcar : virtual public vehicle//(2)機動車類的虛基類也為車輛類

;class motorcycle: public bicycle,public motorcar //(3)電單車類的基類為自行車類和機動車類

;vehicle::vehicle(int maxs, int w) //建構函式,初始時,當前速度總為0且處在停車狀態

void vehicle::start() //由rest狀態到running, 初速為1

}void vehicle::speed_up() //加速,呼叫1次,速度加1

bicycle::bicycle(int maxs, int w, int h):vehicle(maxs,w)

motorcar::motorcar(int maxs, int w, int s, int p):vehicle(maxs,w)

void motorcar::addpassenger(int p)//增加搭載的乘客,超員要拒載,有人下車時,p為負數。當然車上乘客至少有1個(司機)。只有車停穩後才能上下客。

return 0;

}

虛基類 虛函式和純虛基類

首先看乙個例子 class base class child1 public base class child2 public base void main else p print 函式呼叫的時候,檢視虛表,根據p的位址首先從虛表裡面查詢要呼叫的函式 這裡呼叫child2的print 函式 ret...

虛基類釋義

include stdafx.h include include using namespace std class person class teacher virtual public person class student virtual public person class gradua...

虛基類釋義

include stdafx.h include include using namespace std class person class teacher virtual public person class student virtual public person class gradua...