summaryrefslogtreecommitdiff
path: root/Ianimal/Ianimal.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Ianimal/Ianimal.cpp')
-rw-r--r--Ianimal/Ianimal.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/Ianimal/Ianimal.cpp b/Ianimal/Ianimal.cpp
new file mode 100644
index 0000000..5aa70cd
--- /dev/null
+++ b/Ianimal/Ianimal.cpp
@@ -0,0 +1,67 @@
+#include <string>
+#include <iostream>
+#include <map>
+#include <vector>
+
+using namespace std;
+
+class Node { //abstract base class
+ public:
+ virtual string get_name() = 0; //signal output
+};
+
+class A_node: public Node {
+ public:
+ string get_name(){ return "A"; };
+ string get_occupation(){ return "an A"; };
+};
+
+class B_node: public Node {
+ public:
+ string get_name(){ return "B"; };
+ string get_occupation(){ return "a B"; };
+};
+
+class C_node: public Node {
+ public:
+ string get_name(){ return "C"; };
+ string get_occupation(){ return "a C"; };
+};
+
+class Node_factory {
+ public:
+ template <typename T>
+ T* clone(T* proto) {
+ return new T();
+ };
+ Node_factory() {
+ add_type("A",new A_node());
+ add_type("B",new B_node());
+ add_type("C",new C_node());
+ }
+ Node *get_node(string type) {
+ if (type=="A") return new A_node();
+ if (type=="B") return new B_node();
+ if (type=="C") return new C_node();
+ };
+ map<string,Node*> type_map;
+ void add_type(string type,Node *proto){
+ type_map[type]=proto;
+ };
+ Node *create(string type) {
+ if (type_map.find(type)!=type_map.end()) {
+ return (Node*)clone(type_map[type]);
+ }
+ }
+};
+
+int main() {
+ Node_factory f=Node_factory();
+ vector<Node*> nodes;
+ nodes.push_back(f.get_node("A"));
+ nodes.push_back(f.get_node("B"));
+ nodes.push_back(f.get_node("C"));
+ for (auto &i: nodes) {
+ cout << i->get_name() << endl;
+ }
+} \ No newline at end of file