fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class car {
  6. private:
  7. string brand;
  8. int speed = 0;
  9. int maxSpeed;
  10.  
  11. public:
  12. car(string b, int mx) :brand(b), maxSpeed(mx) {};
  13.  
  14. void accelerate(int amount);
  15.  
  16. void brake(int amount);
  17.  
  18. int getSpeed() const {
  19. return speed;
  20. }
  21. string getname() const { //add by me cause i need the brand in final task, or i could build its own fuction that print status
  22. return brand;
  23. }
  24. bool isMoving() const {
  25. return speed > 0;
  26. }
  27. void showStatus() const {
  28. cout << brand << ": " << speed << "/" << maxSpeed << " km/h\n";
  29. }
  30. ~car() {
  31. cout << brand << " parked\n";
  32. }
  33. };
  34. void car::accelerate(int amount) {
  35. if (amount <= 0) return;
  36. (speed + amount < maxSpeed ? speed += amount : speed = maxSpeed);
  37. }
  38. void car::brake(int amount) {
  39. if (amount <= 0)return;
  40. (speed - amount > 0 ? speed -= amount : speed = 0);
  41. }
  42.  
  43. int main() {
  44. car a("Toyota", 120), b("BMW", 200);
  45. a.accelerate(70);
  46. a.accelerate(70);
  47. b.accelerate(90);
  48. b.brake(30);
  49. b.brake(100);
  50. a.showStatus();
  51. b.showStatus();
  52. if (a.isMoving())
  53. cout << a.getname() << " is moving\n";
  54. else
  55. cout << a.getname() << " is stopped\n";
  56.  
  57. if (b.isMoving())
  58. cout << b.getname() << " is moving\n";
  59. else
  60. cout << b.getname() << " is stopped\n";
  61.  
  62. return 0;
  63. }
  64.  
  65.  
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout
Toyota: 120/120 km/h
BMW: 0/200 km/h
Toyota is moving
BMW is stopped
BMW parked
Toyota parked