/////////////////////////////////////////////////////////////// // factoryb.cpp - defines interface and factory // // Exam2:Q7 // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 05 // /////////////////////////////////////////////////////////////// #include #include template class HeadAndTail : public T { public: HeadAndTail(const T& t) : T(t) {} virtual ~HeadAndTail() {} static U* Create(const T& t); virtual U head(unsigned int pos) = 0; virtual U tail(unsigned int pos) = 0; }; template U* HeadAndTail::Create(const T& t) { return new U(t); } class stringPlus; typedef HeadAndTail HandT; class stringPlus : public HandT { public: stringPlus(const std::string& s) : HandT(s) {} virtual stringPlus head(unsigned int pos) { std::string temp = substr(0,pos); return temp; } virtual stringPlus tail(unsigned int pos) { return substr(pos,size()-pos-1); } }; void main() { std::string msg = "Now is the hour for all good men to ... "; HandT* pHAT = HandT::Create(msg); std::cout << "\n " << pHAT->head(msg.size()).c_str(); std::cout << "\n " << pHAT->head(7).c_str(); std::cout << "\n " << pHAT->tail(7).c_str(); std::cout << "\n\n"; }