Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Automobile {
- public:
- Automobile(int gaslevel, int velocity, int tanksize);
- void accelerate();
- void halt();
- void decelerate();
- void filltank();
- void consumegas();
- int checkspeed();
- int checkgas();
- private:
- int _gaslevel;
- int _velocity;
- int _tanksize;
- }
- // creating functions for the object... in other words object members that are METHODS
- Automobile::Automobile(int gaslevel, int velocity, int tanksize) {
- _gaslevel = gaslevel;
- _velocity = velocity;
- _tanksize = tanksize;
- }
- Automobile::accelerate() {
- _velocity++;
- }
- Automobile::halt() {
- _velocity = 0;
- }
- Automobile::decelerate() {
- _velocity++;
- }
- Automobile::filltank() {
- _gaslevel = _tanksize;
- }
- Automobile::consumegas() {
- gaslevel--;
- }
- Automobile::checkspeed() {
- return _velocity;
- }
- Automobile::checkgas() {
- return _gaslevel;
- }
- void setup() {
- Serial.begin(9600);
- newcar = Automobile(100,0,100);
- oldcar = Automobile(4,45,100);
- }
- void loop() {
- if (random(1,4) == 1) {
- newcar.accelerate();
- }
- if (random(1,4) == 2) {
- newcar.decelerate();
- }
- newcar.consumegas();
- if (newcar.checkgas() < 1) {
- newcar.halt();
- newcar.filltank();
- }
- Serial.print(" velocity=");
- Serial.print(newcar.checkspeed());
- Serial.print(" gaslevel=");
- Serial.print(newcar.checkgas());
- Serial.println;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement