Advertisement
jerryturcios08

Assignment 2 BCS 370

Sep 16th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class CuteRobot
  4. {
  5. private:
  6.     int m_position;
  7.  
  8. public:
  9.     CuteRobot()
  10.     {
  11.         m_position = 0;
  12.     }
  13.  
  14.     CuteRobot(int position)
  15.     {
  16.         m_position = position;
  17.     }
  18.  
  19.     int getPosition()
  20.     {
  21.         return m_position;
  22.     }
  23.  
  24.     void setPosition(int position)
  25.     {
  26.         m_position = position;
  27.     }
  28.  
  29.     CuteRobot &move(int steps)
  30.     {
  31.         m_position += steps;
  32.         return *this;
  33.     }
  34.  
  35.     void meet(CuteRobot *cr)
  36.     {
  37.         int distance;
  38.         // Examples
  39.         // cr 29     this 17    29-17 = 12
  40.         // 17-29 = -12
  41.         // cr -12    this -2    (-12)-(-2) = -10      (-12)+10 = -2
  42.         // -2 - (-12) = -2 + 12 = 10
  43.         distance = this->getPosition() - cr->getPosition();
  44.         cr->move(distance);
  45.     }
  46. };
  47.  
  48. int main(int argc, char const *argv[])
  49. {
  50.     CuteRobot c1(0), c2(1), c3(2), c4(3), c5(4);
  51.  
  52.     CuteRobot robots[] = {c1, c2, c3, c4, c5};
  53.  
  54.     for (int i = 1; i < 5; i++)
  55.     {
  56.         robots[0].meet(&robots[i]);
  57.     }
  58.  
  59.     c1.move(5).move(30).move(-22);
  60.     c2.move(10).move(-3).move(2);
  61.  
  62.     // Array equivalent
  63.     // robots[0]
  64.     //     .move(10);
  65.  
  66.     c1.meet(&c2);
  67.  
  68.     if (c1.getPosition() == c2.getPosition())
  69.     {
  70.         std::cout << "They are on the same spot\n";
  71.         std::cout << c1.getPosition() << std::endl;
  72.         std::cout << c2.getPosition() << std::endl;
  73.     }
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement