summaryrefslogtreecommitdiff
path: root/tests/exec/vehicles.cpp
diff options
context:
space:
mode:
authorAlex AUVOLAT <alexis211@gmail.com>2013-10-29 17:42:34 +0100
committerAlex AUVOLAT <alexis211@gmail.com>2013-10-29 17:42:34 +0100
commit8f1093f0e00f9b1df7ce343a879303fd56a95d08 (patch)
tree6aaf0720c2093ba05cb81ba7f95b4e9808b3ecab /tests/exec/vehicles.cpp
downloadLPC-Projet-8f1093f0e00f9b1df7ce343a879303fd56a95d08.tar.gz
LPC-Projet-8f1093f0e00f9b1df7ce343a879303fd56a95d08.zip
First commit.
Diffstat (limited to 'tests/exec/vehicles.cpp')
-rw-r--r--tests/exec/vehicles.cpp78
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/exec/vehicles.cpp b/tests/exec/vehicles.cpp
new file mode 100644
index 0000000..51ab55d
--- /dev/null
+++ b/tests/exec/vehicles.cpp
@@ -0,0 +1,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:
+*/
+