Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class CuteRobot
- {
- private:
- int m_position;
- public:
- CuteRobot()
- {
- m_position = 0;
- }
- CuteRobot(int position)
- {
- m_position = position;
- }
- int getPosition()
- {
- return m_position;
- }
- void setPosition(int position)
- {
- m_position = position;
- }
- CuteRobot &move(int steps)
- {
- m_position += steps;
- return *this;
- }
- void meet(CuteRobot *cr)
- {
- int distance;
- // Examples
- // cr 29 this 17 29-17 = 12
- // 17-29 = -12
- // cr -12 this -2 (-12)-(-2) = -10 (-12)+10 = -2
- // -2 - (-12) = -2 + 12 = 10
- distance = this->getPosition() - cr->getPosition();
- cr->move(distance);
- }
- };
- int main(int argc, char const *argv[])
- {
- CuteRobot c1(0), c2(1), c3(2), c4(3), c5(4);
- CuteRobot robots[] = {c1, c2, c3, c4, c5};
- for (int i = 1; i < 5; i++)
- {
- robots[0].meet(&robots[i]);
- }
- c1.move(5).move(30).move(-22);
- c2.move(10).move(-3).move(2);
- // Array equivalent
- // robots[0]
- // .move(10);
- c1.meet(&c2);
- if (c1.getPosition() == c2.getPosition())
- {
- std::cout << "They are on the same spot\n";
- std::cout << c1.getPosition() << std::endl;
- std::cout << c2.getPosition() << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement