|
// composites_visitors.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "NodeBase.h"
#include "VisitorBase.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// Set up a simple composite tree
CNodeBase *head = new CNodeBase;
CNodeBase *child1 = new CNodeDerived1;
head->push_back( child1 );
CNodeBase *child2 = new CNodeDerived2;
head->push_back( child2 );
child1->push_back( new CNodeDerived1 );
child1->push_back( new CNodeDerived2 );
child2->push_back( new CNodeDerived2 );
child2->push_back( new CNodeDerived1 );
child2->push_back( new CNodeBase );
// Examples of shallow iterators
cout << "Shallow Iteration from Head" << endl;
CNodeBase::shallow_iterator iter1 = head->begin_shallow();
for ( ; iter1 != head->end_shallow(); ++iter1 )
cout << (*iter1)->GetId() << " " << (*iter1)->GetType() << endl;
cout << "Shallow Iteration from Child2" << endl;
for ( iter1 = child2->begin_shallow(); iter1 != child2->end_shallow(); ++iter1 )
cout << (*iter1)->GetId() << " " << (*iter1)->GetType() << endl;
// Examples of deep iterators
cout << "Deep Iteration from Head" << endl;
CNodeBase::deep_iterator iter2 = head->begin_deep();
for ( ; iter2 != head->end_deep(); ++iter2 )
cout << (*iter2)->GetId() << " " << (*iter2)->GetType() << endl;
cout << "Deep Iteration from Child1" << endl;
for ( iter2 = child1->begin_deep(); iter2 != child1->end_deep(); ++iter2 )
cout << (*iter2)->GetId() << " " << (*iter2)->GetType() << endl;
// Examples of visitors
cout << "Visiting entire tree using a top to bottom strategy" << endl;
CVisitorDerived1TopToBottom visitor1;
visitor1( head );
cout << "Visiting entire tree using a bottom to top strategy" << endl;
CVisitorDerived2BottomToTop visitor2;
visitor2( head );
cout << "Visiting entire tree using a left to right strategy" << endl;
CVisitorDerived2LeftToRight visitor3;
visitor3( head );
// Wait for user to hit something
char c;
cin >> c;
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I started programming on 8 bit machines as a teenager, writing my first compiled programming language before I was 16. I went on to study Engineering and Computer Science at Oxford University, getting a first and the University Prize for the best results in Computer Science. Since then I have worked in a variety of roles, involving systems management and development management on a wide variety of platforms. Now I manage a software development company producing CAD software for Windows using C++.
My 3 favourite reference books are: Design Patterns, Gamma et al; The C++ Standard Library, Josuttis; and Computer Graphics, Foley et al.
Outside computers, I am also the drummer in a band, The Unbelievers and we have just released our first album. I am a pretty good juggler and close up magician, and in my more insane past, I have cycled from Spain to Eastern Turkey, and cycled across the Namib desert.