标签:Transportation 桥接 void 模式 class transportation public Road
[实验任务一]:两个维度的桥接模式
用桥接模式实现在路上开车这个问题,其中,车可以是car或bus,路可以是水泥路或沥青路。
#include <iostream>
using namespace std;
class Transportation
{
public:
virtual void drive(){}
};
class Car:public Transportation
{
public:
void drive()
{
cout<<"小轿车";
}
};
class Bus:public Transportation
{
public:
void drive()
{
cout<<"大巴";
}
};
class Road
{
public:
Transportation* transportation;
Road(Transportation* transportation)
{
this->transportation=transportation;
}
virtual void driveOnRoad(){}
};
class CementRoad:public Road
{
public:
CementRoad(Transportation* transportation):Road(transportation)
{
}
void driveOnRoad()
{
transportation->drive();
cout<<"行驶在水泥路"<<endl;
}
};
class BituminousRoad:public Road
{
public:
BituminousRoad(Transportation* transportation):Road(transportation)
{
}
void driveOnRoad()
{
transportation->drive();
cout<<"行驶在柏油路"<<endl;
}
};
int main()
{
Transportation* transportation;
Car car=Car();
transportation=&car;
CementRoad(transportation).driveOnRoad();
}
标签:Transportation,
桥接,
void,
模式,
class,
transportation,
public,
Road
From: https://www.cnblogs.com/zhouzhengyang/p/17819411.html