#include #include using namespace std; typedef const char * String; #define MAX_CHILDREN 10 class Component{ public: String name; Component(String name):name(name){ } virtual void Add(Component *c)=0; virtual void Remove(Component *c)=0; virtual void Display(int depth)=0; virtual ~Component(){ //cout<<"Calling ~Component()"<Display(depth+2); } } }; class Leaf:public Component{ public: Leaf(String name):Component(name){} void Add(Component *c){ //cout<<"Cannot add to a leaf"<Add(new Leaf("Leaf A")); root->Add(new Leaf("Leaf B")); Composite *comp = new Composite("Composite X"); comp->Add(new Leaf("Leaf XA")); comp->Add(new Leaf("Leaf XB")); root->Add(comp); root->Add(new Leaf("Leaf C")); //Add and remove a leaf Leaf *leaf = new Leaf("Leaf D"); root->Add(leaf); root->Remove(leaf); delete leaf; //Recursively display tree root->Display(1); delete root; return 0; } /*=================== OUTPUT =================== -root ---Leaf A ---Leaf B ---Composite X -----Leaf XA -----Leaf XB ---Leaf C =================================================*/