From 55a63ac09264a38fafdf4a218e86b598030fb47c Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Fri, 8 Mar 2013 18:04:09 +0000 Subject: REST api working --- Ianimal/Ianimal.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Ianimal/Ianimal.cpp (limited to 'Ianimal/Ianimal.cpp') 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 +#include +#include +#include + +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 + 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 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 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 -- cgit v1.2.3