C++ code to iterate over composite pattern