Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <string.h>
  4. using namespace std;
  5.  
  6. class Car {
  7.  
  8. protected:
  9. int pos;
  10.  
  11. public:
  12. Car(); // По умолчанию, позиция машины равна 0
  13.  
  14. void move(int direction); // Сдвинуться по числовой прямой на расстояние direction
  15. // Если direction < 0, то влево
  16. // Если direction > 0, то вправо
  17.  
  18. int get_position(); // Узнать текущую позицию автомобиля
  19. };
  20.  
  21. void Car::move(int direction)
  22. {
  23. pos += direction;
  24. }
  25.  
  26. int Car::get_position()
  27. {
  28. return pos;
  29. }
  30. Car::Car()
  31. {
  32. pos = 0;
  33. }
  34.  
  35. int main()
  36. {
  37. Car car;
  38. int cnt; cin >> cnt;
  39. for (int i=0; i<cnt; i++)
  40. {
  41. char com[1024];
  42. cin >> com;
  43.  
  44. if (!strcmp(com, "MOVE"))
  45. {
  46. int num; cin >> num;
  47. car.move(num);
  48. }
  49. if (!strcmp(com, "POSITION"))
  50. {
  51. cout << car.get_position() << endl;
  52. }
  53.  
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement