Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <variant>
- #include <vector>
- class Electricity {
- public:
- using Amount = double;
- };
- class Petrol {
- public:
- using Amount = double;
- };
- struct Point {
- double x;
- double y;
- };
- class Path {
- public:
- Point lastPoint() const { return *(pVector_.rbegin()); }
- private:
- std::vector<Point> pVector_;
- };
- ////////////////////////////////////////////////////////////////////////////////
- // ITransport - transport interface.
- class ITransport {
- public:
- virtual void move(const Path& p) = 0;
- virtual double estimateTime(const Path& path) = 0;
- /*Some more methods.*/
- };
- ////////////////////////////////////////////////////////////////////////////////
- // Transport - base transport implementation
- class Transport : public virtual ITransport {
- public:
- virtual void move(const Path& p) override {
- currPoint_ = p.lastPoint();
- }
- /*Some more methods.*/
- private:
- Point currPoint_;
- };
- ////////////////////////////////////////////////////////////////////////////////
- // SelfMovingTransport
- class ISelfMovingTransport : public virtual ITransport {
- /*All the same.*/
- };
- ////////////////////////////////////////////////////////////////////////////////
- // SelfMovingTransport
- template <typename EnergySource>
- class SelfMovingTransport : public Transport {
- /*Not interesting.*/
- };
- ////////////////////////////////////////////////////////////////////////////////
- // ICar - car interface.
- class ICar : public virtual ISelfMovingTransport {
- public:
- virtual void visitCarService() = 0;
- /*All the same.*/
- };
- ////////////////////////////////////////////////////////////////////////////////
- // Car
- template <typename EnergySource>
- class Car : public SelfMovingTransport <EnergySource>, public ICar {
- public:
- /*Some more methods with logic for cars.*/
- };
- ////////////////////////////////////////////////////////////////////////////////
- // ICECar
- class ICECar : public Car<Petrol> {
- public:
- virtual void move(const Path& p) override {
- Transport::move(p);
- /*Some special methods for ICECar.*/
- }
- virtual void visitCarService() override {
- /*Visit closest ICECar service.*/
- }
- virtual double estimateTime(const Path& path) override {
- /*Estimate time specially for path.*/
- }
- /*Some special methods for ICECar.*/
- private:
- Petrol::Amount petrol_;
- };
- ////////////////////////////////////////////////////////////////////////////////
- // ElectricCar
- class ElectricCar : public Car<Electricity> {
- public:
- virtual void move(const Path& p) override {
- Transport::move(p);
- /*Some special methods for ElectricCar.*/
- }
- virtual void visitCarService() override {
- /*Visit closest ElectricCar service.*/
- }
- virtual double estimateTime(const Path& path) override {
- /*Estimate time specially for path.*/
- }
- /*Some special methods for ElectricCar.*/
- private:
- Electricity::Amount charge_;
- };
- int main() {
- ElectricCar testCar = ElectricCar();
- auto* carPtr = static_cast<ICar*>(&testCar);
- auto* electricCarPtr1 = static_cast<ElectricCar*>(carPtr);
- auto* selfMovingTransportPtr = static_cast<ISelfMovingTransport*>(&testCar);
- auto* electricCarPtr2 = static_cast<ElectricCar*>(selfMovingTransportPtr);
- auto* transportPtr = static_cast<ITransport*>(&testCar);
- auto* electricCarPtr3 = static_cast<ElectricCar*>(transportPtr);
- return 0;
- }
Add Comment
Please, Sign In to add comment