/* Use C ++ to implement a limitedclass class. This class can be instantiated up to three times and cannot be inherited */# include <iostream> using namespace STD; Class limitedclass {public: static limitedclass * getinstance (); static void setcnt (INT); // you can specify the number of Instantiation times ~ Limitedclass (); // other methods; private: limitedclass (); // private constructor, cannot inherit static int CNT; // instantiation times}; int limitedclass :: CNT = 0; // Number of Instantiation times out-of-class definition of limitedclass * limitedclass: getinstance () {If (CNT> 0) {-- CNT; return New limitedclass ();} elsereturn NULL;} limitedclass: limitedclass () {cout <"limitedclass constructor !!! "<Endl;} limitedclass ::~ Limitedclass () {cout <"limitedclass destructor !!! "<Endl;} void limitedclass: setcnt (int n) {CNT = N;} // Test Program Int main () {limitedclass: setcnt (3); limitedclass * L1 = limitedclass: getinstance (); limitedclass * L2 = limitedclass: getinstance (); limitedclass * l3 = limitedclass: getinstance (); If (L1! = NULL & L2! = NULL & L3! = NULL) {cout <"successful instantiation of 3 objects" <Endl;} limitedclass * L4 = limitedclass: getinstance (); If (null = L4) cout <"The fourth instance cannot be instantiated" <Endl; Delete L1; Delete L2; Delete l3; return 0 ;}