#include #include using namespace std; // Forward declarations class cl1to10; class cl2to5; class cl8to10; class cl3; class cl10; class cluber; class cl1to10 { public: cl1to10 () : classtype("cl1to10") { } static cl1to10* classify (int i) { // I know about nothing, I like 1 to 10 if (i >= 1 && i <= 10) return new cl1to10; else return 0; } static int depth (int i, int dep = 0) { // I like numbers 1 to 10 if (i >= 1 && i <= 10) return dep; // end of the line return -1; } string classtype; }; class cl2to5 : public cl1to10 { public: cl2to5 () { classtype = "cl2to5"; } static cl1to10* classify (int i) { // I know about cl1to10 // Do I like this number?? if (i >= 2 && i <= 5) return new cl2to5; else return cl1to10::classify (i); } static int depth (int i, int dep = 0) { // I like numbers 2 to 5 if (i >= 2 && i <= 5) return dep; // I know about cl1to10 else return cl1to10::depth (i, dep+1); } }; class cl8to10 : public cl1to10 { public: cl8to10 () { classtype = "cl8to10"; } static cl1to10* classify (int i) { // I know about cl1to10 // Do I like this number?? if (i >= 8 && i <= 10) return new cl8to10; else return cl1to10::classify (i); } static int depth (int i, int dep = 0) { // I like numbers 8 to 10 if (i >= 8 && i <= 10) return dep; // I know about cl1to10 return cl1to10::depth (i, dep+1); } }; class cl3 : public cl2to5 { public: cl3 () { classtype = "cl3"; } static cl1to10* classify (int i) { // I know about cl2to5 // Do I like this number?? if (i == 3) return new cl3; else return cl2to5::classify (i); } static int depth (int i, int dep = 0) { // I like number 3 if (i == 3) return dep; // I know about cl2to5 return cl2to5::depth (i, dep+1); } }; class cl10 : public cl8to10 { public: cl10 () { classtype = "cl10"; } static cl1to10* classify (int i) { // I know about cl8to10 if (i == 10) return new cl10; else return cl8to10::classify (i); } static int depth (int i, int dep = 0) { // I like number 10 if (i == 10) return dep; // I know about cl8to10 return cl8to10::depth (i, dep+1); } }; cl1to10* classify (int i) { int cl10depth = cl10::depth(i); int cl3depth = cl3::depth(i); // I know about cl10 and cl3 if (cl10depth < cl3depth) return cl10::classify (i); else return cl3::classify (i); } int main () { for (int i = 1; i <= 11; ++i) { cl1to10 *cl = classify (i); if (cl) { cout << i << ":\t" << cl->classtype << endl; delete cl; } else { cout << i << ":\tnoclass" << endl; } } }