Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- template <class T>
- class Parent{
- protected:
- Parent *parinte;
- public:
- virtual void output() = 0;
- virtual void SetParent(Parent *instance){
- this->parinte = instance;
- }
- };
- template <class T>
- class Leaf1 : public Parent<T>{
- private:
- T LeafItem;
- public:
- Leaf1(){};
- Leaf1(T item){
- this->LeafItem = item;
- }
- void SetItem(T item){
- this->LeafItem = item;
- }
- T GetItem(){
- return this->LeafItem;
- }
- void output(){
- std::cout << this->LeafItem << " ";
- }
- };
- template <class T>
- class Leaf2 : public Parent<T>{
- private:
- T Leaf2Item;
- public:
- Leaf2(){};
- Leaf2(T item){
- this->Leaf2Item = item;
- }
- void SetItem(T item){
- this->Leaf2Item = item;
- }
- T GetItem(){
- return this->Leaf2Item;
- }
- void output(){
- std::cout << this->Leaf2Item << " ";
- }
- };
- template <class T>
- class Composite : public Parent<T>{
- private:
- std::vector<Parent *> Children;
- public:
- void AddChild(Parent * kid){
- Children.push_back(kid);
- kid->SetParent(this);
- }
- void output(){
- for (int i = 0; i < Children.size(); ++i){
- Children[i]->output();
- }
- }
- };
- template <class T>
- void main(void){
- Composite<int> *Composer[20];
- for (int i = 0; i < 10; ++i){
- Composer[i]->AddChild(new Leaf1(i));
- ++i;
- Composer[i]->AddChild(new Leaf2(i));
- }
- for (int i = 0; i < 5; ++i){
- Composer[i]->output();
- }
- }
Add Comment
Please, Sign In to add comment