gogagum

Inheritance

Jul 31st, 2021 (edited)
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include <variant>
  2. #include <vector>
  3.  
  4. class Electricity {
  5. public:
  6.     using Amount = double;
  7. };
  8.  
  9. class Petrol {
  10. public:
  11.     using Amount = double;
  12. };
  13.  
  14. struct Point {
  15.     double x;
  16.     double y;
  17. };
  18.  
  19. class Path {
  20. public:
  21.     Point lastPoint() const { return *(pVector_.rbegin()); }
  22. private:
  23.     std::vector<Point> pVector_;
  24. };
  25.  
  26. ////////////////////////////////////////////////////////////////////////////////
  27. // ITransport - transport interface.
  28. class ITransport {
  29.   public:
  30.     virtual void move(const Path& p) = 0;
  31.     virtual double estimateTime(const Path& path) = 0;
  32.     /*Some more methods.*/
  33. };
  34.  
  35. ////////////////////////////////////////////////////////////////////////////////
  36. // Transport - base transport implementation
  37. class Transport : public virtual ITransport {
  38.   public:
  39.     virtual void move(const Path& p) override {
  40.         currPoint_ = p.lastPoint();
  41.     }
  42.     /*Some more methods.*/
  43.   private:
  44.     Point currPoint_;
  45. };
  46.  
  47. ////////////////////////////////////////////////////////////////////////////////
  48. // SelfMovingTransport
  49. class ISelfMovingTransport : public virtual ITransport {
  50.  /*All the same.*/
  51. };
  52.  
  53. ////////////////////////////////////////////////////////////////////////////////
  54. // SelfMovingTransport
  55. template <typename EnergySource>
  56. class SelfMovingTransport : public Transport {
  57.   /*Not interesting.*/
  58. };
  59.  
  60. ////////////////////////////////////////////////////////////////////////////////
  61. // ICar - car interface.
  62. class ICar : public virtual ISelfMovingTransport {
  63. public:
  64.     virtual void visitCarService() = 0;
  65.  /*All the same.*/
  66. };
  67.  
  68. ////////////////////////////////////////////////////////////////////////////////
  69. // Car
  70. template <typename EnergySource>
  71. class Car : public SelfMovingTransport <EnergySource>, public ICar {
  72.   public:
  73.     /*Some more methods with logic for cars.*/
  74. };
  75.  
  76. ////////////////////////////////////////////////////////////////////////////////
  77. // ICECar
  78. class ICECar : public Car<Petrol> {
  79.   public:
  80.     virtual void move(const Path& p) override {
  81.         Transport::move(p);
  82.         /*Some special methods for ICECar.*/
  83.     }
  84.     virtual void visitCarService() override {
  85.       /*Visit closest ICECar service.*/
  86.     }
  87.     virtual double estimateTime(const Path& path) override {
  88.       /*Estimate time specially for path.*/
  89.     }
  90.     /*Some special methods for ICECar.*/
  91.   private:
  92.     Petrol::Amount petrol_;
  93. };
  94.  
  95. ////////////////////////////////////////////////////////////////////////////////
  96. // ElectricCar
  97. class ElectricCar : public Car<Electricity> {
  98.   public:
  99.     virtual void move(const Path& p) override {
  100.         Transport::move(p);
  101.         /*Some special methods for ElectricCar.*/
  102.     }
  103.     virtual void visitCarService() override {
  104.       /*Visit closest ElectricCar service.*/
  105.     }
  106.     virtual double estimateTime(const Path& path) override {
  107.       /*Estimate time specially for path.*/
  108.     }
  109.     /*Some special methods for ElectricCar.*/
  110.   private:
  111.     Electricity::Amount charge_;
  112. };
  113.  
  114. int main() {
  115.     ElectricCar testCar = ElectricCar();
  116.     auto* carPtr = static_cast<ICar*>(&testCar);
  117.     auto* electricCarPtr1 = static_cast<ElectricCar*>(carPtr);
  118.     auto* selfMovingTransportPtr = static_cast<ISelfMovingTransport*>(&testCar);
  119.     auto* electricCarPtr2 = static_cast<ElectricCar*>(selfMovingTransportPtr);
  120.     auto* transportPtr = static_cast<ITransport*>(&testCar);
  121.     auto* electricCarPtr3 = static_cast<ElectricCar*>(transportPtr);
  122.    
  123.     return 0;
  124. }
Add Comment
Please, Sign In to add comment