Files Classes Functions Hierarchy
00001 00002 #include <cassert> 00003 #include <iostream> 00004 #include <string> 00005 using namespace std; 00006 00007 #include <simtemplatedvirtualfunc.h> 00008 #include <visitorprint.h> 00009 #include <visitorprint2.h> 00010 00011 00012 00013 class quat 00014 { 00015 public: 00016 00017 double a; 00018 double x; 00019 double y; 00020 double z; 00021 00022 quat(double _a, double _x, double _y, double _z) 00023 : a(_a), x(_x), y(_y), z(_z) {} 00024 00025 ostream & print(ostream & os) const 00026 { return os << "<" << a << ",(" << x << "," << y <<"," << z << ")>"; } 00027 }; 00028 00029 00030 ostream & operator << (ostream & os, quat const & q) 00031 { return q.print(os); } 00032 00033 00034 string simtemplatedvirtualfunc::doc[] = 00035 { 00036 "", 00037 "Demo with visitorPrint function with many types.", 00038 "Demo of two templated virtual functions on the same data structures." 00039 }; 00040 00041 00042 00043 void simtemplatedvirtualfunc::test01() 00044 { 00045 ElementA a; 00046 ElementB b; 00047 Element* e; 00048 cout << "Element A derived from Element" << endl; 00049 cout << "Element B derived from Element" << endl; 00050 00051 e=&a; 00052 00053 visitorPrint<int> pr(20); 00054 // cout << pr << endl; 00055 00056 cout << "Let e point to an ElementA object" << endl; 00057 cout << "e->accept(pr)" << endl; 00058 e->accept(pr); 00059 00060 cout << "e->accept( visitorPrint<int>(30) )" << endl; 00061 e->accept( visitorPrint<int>(30) ); 00062 cout << "e->accept(visitorPrint<string>(\"hat mat cat\") )"; 00063 e->accept( visitorPrint<string>("hat mat cat") ); 00064 cout << "Let e point to a ElementB" << endl; 00065 e=&b; 00066 cout << "e->accept(pr)" << endl; 00067 e->accept(pr); 00068 cout << "e->accept( visitorPrint<int>(30) )" << endl; 00069 e->accept( visitorPrint<int>(30) ); 00070 cout << "e->accept( visitorPrint<string>(\"hat mat cat\") )"; 00071 e->accept( visitorPrint<string>("hat mat cat") ); 00072 00073 quat q1(1.0,2.0,-1.0,5.0); 00074 00075 cout << "e->accept( visitorPrint<quat>(q1) ))"; 00076 e->accept( visitorPrint<quat>(q1) ); 00077 //cout << q1 << endl; 00078 00079 00080 } 00081 00082 void simtemplatedvirtualfunc::test02() 00083 { 00084 ElementA a; 00085 ElementB b; 00086 Element* e; 00087 00088 quat q1(1.0,2.0,-1.0,5.0); 00089 e=&a; 00090 00091 e->accept( visitorPrint<int>(30) ); 00092 e->accept( visitorPrint<quat>(q1) ); 00093 e->accept( visitorPrint<string>("hat mat cat") ); 00094 00095 00096 e->accept( visitorPrint2<int>(30) ); 00097 e->accept( visitorPrint2<quat>(q1) ); 00098 e->accept( visitorPrint2<string>("hat mat cat") ); 00099 } 00100 00101 00102
1.5.8