组合模式 C++实现

//
//  main.cpp
//  composite_pattern
//
//  Created by apple on 2019/4/16.
//  Copyright © 2019年 apple. All rights reserved.
//

#include 
#include "list"
#include "string"
using namespace std;


class Composite
{
private:
    string name;
    list composites;
public:
    Composite(string name) {this->name =  name;}
    void add_composite(Composite *composite)
    {
        composites.push_back(composite);
    }
    list get_Composites()
    {
        return composites;
    }
    void print_Name() {cout<::iterator item = composites.begin(); item != composites.end(); ++item)
        {
            delete *item;
            *item = NULL;
        }
        composites.clear();
    }
};

int main(int argc, const char * argv[]) {
    Composite *composite = new Composite("姓名簿");
    composite->add_composite(new Composite("张三"));
    composite->add_composite(new Composite("李四"));
    composite->add_composite(new Composite("王五"));
    composite->add_composite(new Composite("陈六"));
    composite->print_Name();
    list composites = composite->get_Composites();
    for (list::iterator item = composites.begin(); item != composites.end(); ++item)
    {
        (*item)->print_Name();
    }
    delete composite;
    return 0;
}
姓名簿
张三
李四
王五
陈六
Program ended with exit code: 0

你可能感兴趣的:(组合模式 C++实现)