いろいろなテクニック.Nucleo と DISCO-F746 用.

Dependencies:   Array_Matrix mbed

Revision:
0:bb939e0bc6e2
Child:
1:bbb9f0c3e523
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PolyMorphism.hpp	Sun Oct 15 11:41:48 2017 +0000
@@ -0,0 +1,55 @@
+//------------------------------------------------------------
+//  実行時ポリモーフィズムの例
+//
+//  2016/04/12, Copyright (c) 2016 MIKAMI, Naoki
+//------------------------------------------------------------
+
+#include "mbed.h"
+
+class Base
+{
+public:
+    Base() {}
+    virtual void Execute() { printf("Base class\r\n"); }
+};
+
+class Derived1 : public Base
+{
+public:
+    Derived1() {}
+    virtual void Execute() { printf("Derived1 class\r\n"); }
+};
+
+class Derived2 : public Base
+{
+public:
+    Derived2() {}
+    virtual void Execute() { printf("Derived2 class\r\n"); }
+};
+
+void Print(Base *obj) { obj->Execute(); }
+
+void MyPolyMorphism()
+{
+    Base base;
+    Derived1 drv1;
+    Derived2 drv2;
+    
+    // 例 1
+    printf("\r\n");
+    Base *func[3] = { &base, &drv1, &drv2 };
+    for (int n=0; n<3; n++) func[n]->Execute();
+    
+    // 例 2
+    printf("\r\n");
+    Base *ptr;
+    ptr = &drv2;
+    ptr->Execute();
+    
+    ptr = &drv1;
+    ptr->Execute();
+    
+    // 例 3
+    printf("\r\n");
+    Print(&drv2);
+}
\ No newline at end of file