Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5. typedef const char * String;
  6. #define MAX_CHILDREN 10
  7.  
  8. class Component{
  9. public:
  10.     String name;
  11.     Component(String name):name(name){}
  12.     virtual void Add(Component *c)=0;
  13.     virtual void Remove(Component *c)=0;
  14.     virtual void Display(int depth)=0;
  15. };
  16.  
  17. class Composite:public Component{
  18.     Component *children[MAX_CHILDREN];
  19. public:
  20.     Composite(String name):Component(name){
  21.         for(int i=0;i<MAX_CHILDREN;i++) children[i]= 0;
  22.     }
  23.     ~Composite(){
  24.         for(int i=0;i<MAX_CHILDREN;i++){
  25.             if (children[i]) delete children[i];
  26.         }
  27.     }
  28.     void Add(Component *component){
  29.         for(int i=0;i<MAX_CHILDREN;i++){
  30.             if(!children[i]){
  31.                 children[i] = component;
  32.                 break;
  33.             }
  34.         }
  35.     }
  36.     void Remove(Component *component){
  37.         Component **pc;
  38.         pc = children;
  39.         for(int i=0;i<MAX_CHILDREN;i++){
  40.             if (pc[i]==component){
  41.                 for(int j=i;j<(MAX_CHILDREN-1);j++){
  42.                     pc[j] = pc[j+1];
  43.                 }
  44.                 pc[MAX_CHILDREN-1] = 0;
  45.                 break;
  46.             }
  47.         }
  48.     }
  49.     void Display(int depth){
  50.         for(int i=0;i<depth;i++) cout<<'-';
  51.         cout<<name<<endl;
  52.         for(int i=0;i<MAX_CHILDREN;i++){
  53.             if (children[i]) children[i]->Display(depth+2);
  54.         }
  55.     }
  56. };
  57.  
  58. class Leaf:public Component{
  59. public:
  60.     Leaf(String name):Component(name){}
  61.  
  62.     void Add(Component *c){
  63.         cout<<"Cannot add to a leaf"<<endl;
  64.     }
  65.  
  66.     void Remove(Component *c){
  67.         cout<<"Cannot remove from a leaf"<<endl;
  68.     }
  69.  
  70.     void Display(int depth){
  71.     for(int i=0;i<depth;i++) cout<<'-';
  72.     cout<<name<<endl;}
  73. };
  74.  
  75. main()
  76. {
  77.     //Create a tree struct
  78.     Component *root = new Composite("root");
  79.     root->Add(new Leaf("Leaf A"));
  80.     root->Add(new Leaf("Leaf B"));
  81.  
  82.     Composite *comp = new Composite("Composite X");
  83.     comp->Add(new Leaf("Leaf XA"));
  84.     comp->Add(new Leaf("Leaf XB"));
  85.  
  86.     root->Add(comp);
  87.     root->Add(new Leaf("Leaf C"));
  88.  
  89.     //add and remove a leaf
  90.     Leaf *leaf = new Leaf("Leaf D");
  91.     root->Add(leaf);
  92.     root->Remove(leaf);
  93.     delete leaf;
  94.  
  95.     //recursively display tree
  96.     root->Display(1);delete root;
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement