summaryrefslogtreecommitdiff
path: root/c,cc/ooc.c
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-06-15 03:50:12 +0200
committerPatrick Simianer <p@simianer.de>2014-06-15 03:50:12 +0200
commit258e1b92ebbfdebefabc120969ab87c3d8b75c3d (patch)
treeef4ab11fe0bf9d720cea23b35711358a8465feeb /c,cc/ooc.c
parentcf3a29feb5887344b6633ead1b4b6d5657a15a4b (diff)
old c,cc examples
Diffstat (limited to 'c,cc/ooc.c')
-rw-r--r--c,cc/ooc.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/c,cc/ooc.c b/c,cc/ooc.c
new file mode 100644
index 0000000..aa20488
--- /dev/null
+++ b/c,cc/ooc.c
@@ -0,0 +1,78 @@
+#include "ooc.h"
+#include "stdio.h"
+#include "string.h"
+
+
+struct van {
+ struct vehicle base;
+ int cubic_size;
+};
+
+struct bus {
+ struct vehicle base;
+ int seats;
+};
+
+struct van*
+make_van()
+{
+ struct van* v = malloc(sizeof(struct van));
+ v->base.type = "van";
+ v->cubic_size = 12;
+ return v;
+}
+
+struct bus*
+make_bus()
+{
+ struct bus* v = malloc(sizeof(struct bus));
+ v->base.type = "bus";
+ v->seats=112;
+ return v;
+}
+
+struct vehicle*
+make_vehicle(const char* type)
+{
+ if(!strcmp(type, "van")) return make_van();
+ if(!strcmp(type, "bus")) return make_bus();
+ return NULL;
+}
+
+void
+do_something_with_a_bus(struct vehicle* v)
+{
+ ((struct bus*)v)->seats = 13;
+}
+
+void
+do_something_with_a_van(struct vehicle* v)
+{
+ ((struct van*)v)->cubic_size = 11;
+}
+
+void
+do_something(struct vehicle* v)
+{
+ if(!strcmp(v->type, "van")) return do_something_with_a_van(v);
+ if(!strcmp(v->type, "bus")) return do_something_with_a_bus(v);
+}
+
+int
+main(void) {
+ struct van my_van;
+ struct vehicle *something = &my_van;
+ my_van.cubic_size = 100;
+ my_van.base.power = 99;
+ printf("%d\n", something->power);
+ printf("%d\n", my_van.base.power);
+
+ struct bus* bus = make_vehicle("bus");
+ printf("%s\n", bus->base.type);
+ printf("%d\n", bus->seats);
+ do_something(bus);
+ printf("%d\n", bus->seats);
+
+ return 0;
+}
+