Advertisement
estephan500

Untitled

May 10th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1.  
  2. class Automobile {
  3.   public:
  4.     Automobile(int gaslevel, int velocity, int tanksize);
  5.     void accelerate();
  6.     void halt();
  7.     void decelerate();
  8.     void filltank();
  9.     void consumegas();
  10.     int checkspeed();
  11.     int checkgas();
  12.   private:
  13.     int _gaslevel;
  14.     int _velocity;
  15.     int _tanksize;
  16. }
  17.  
  18. // creating functions for the object... in other words object members that are METHODS
  19. Automobile::Automobile(int gaslevel, int velocity, int tanksize) {
  20.   _gaslevel = gaslevel;
  21.   _velocity = velocity;
  22.   _tanksize = tanksize;
  23. }
  24.  
  25. Automobile::accelerate() {
  26.   _velocity++;
  27. }
  28.  
  29. Automobile::halt() {
  30.   _velocity = 0;
  31. }
  32.  
  33. Automobile::decelerate() {
  34.   _velocity++;
  35. }
  36.  
  37. Automobile::filltank() {
  38.   _gaslevel = _tanksize;
  39. }
  40.  
  41. Automobile::consumegas() {
  42.   gaslevel--;
  43. }
  44.  
  45. Automobile::checkspeed() {
  46.   return _velocity;
  47. }
  48.  
  49. Automobile::checkgas() {
  50.   return _gaslevel;
  51. }
  52.  
  53.  
  54.  
  55. void setup() {
  56.   Serial.begin(9600);
  57.  
  58.   newcar = Automobile(100,0,100);
  59.   oldcar = Automobile(4,45,100);
  60.  
  61. }
  62.  
  63. void loop() {
  64.  
  65.   if (random(1,4) == 1) {
  66.     newcar.accelerate();
  67.   }
  68.  
  69.   if (random(1,4) == 2) {
  70.     newcar.decelerate();
  71.   }
  72.  
  73.   newcar.consumegas();
  74.  
  75.   if (newcar.checkgas() < 1) {
  76.     newcar.halt();
  77.     newcar.filltank();
  78.   }
  79.  
  80.   Serial.print(" velocity=");
  81.   Serial.print(newcar.checkspeed());
  82.   Serial.print(" gaslevel=");
  83.   Serial.print(newcar.checkgas());
  84.   Serial.println;
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement