summaryrefslogtreecommitdiff
path: root/tests/exec/vehicles.cpp
blob: 51ab55d3d3accfde5231ad0814ba3c161fe50f55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>

class Vehicle {
public:
  int position;
  Vehicle();
  virtual void move(int d);
};

Vehicle::Vehicle() {
  position = 10;
}

void Vehicle::move(int d) {
  std::cout << "vehicle moves\n";
  position = position + d;
}


class Car : public Vehicle {
public:
  // champ position hérité
  int passengers;
  Car();
  // methode move() héritée
  void await(Vehicle &v);
};

Car::Car() {
}

void Car::await(Vehicle &v) {
  std::cout << "await: position = " << v.position << "\n";
  if (v.position < position)
    v.move(position - v.position);
  else
    move(10);
}

class Truck : public Vehicle {
public:
  // champ position hérité
  int load;
  Truck();
  void move(int d);
};

Truck::Truck() {
}

void Truck::move(int d) { // methode move redéfinie
  std::cout << "truck moves\n";
  if (d <= 55) position = position + d; else position = position + 55;
}

int main() {
  Truck t;
  std::cout << "t at " << t.position << "\n";
  Car c;
  c.passengers = 2;
  std::cout << "c at " << c.position << "\n";
  c.move(60);
  std::cout << "c at " << c.position << "\n";
  Vehicle *v = &c; // alias
  v->move(70);
  std::cout << "c at " << c.position << "\n";
  c.await(t);
  std::cout << "t at " << t.position << "\n";
  std::cout << "c at " << c.position << "\n";

}

/*
Local Variables:
compile-command: "make vehicles"
End:
*/