// template_welcome.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include const int MAXS = 100; template class TStack { mType data[MAXS]; int pos; public: TStack() : pos(0) { } ; bool push(mType); bool pop(); bool top(mType&); int size() { return pos }; template friend TStack& operator << (TStack&, mType); // TStack & operator << (mType); }; template TStack& operator << (TStack& s, mType var) { s.push(var); return s; } /* template TStack& TStack::operator << (mType var) { push(var); return *this; }*/ template bool TStack ::push(mType newElem) { if (pos < MAXS) { data[pos++] = newElem; return true; } else return false; } template bool TStack ::pop() { if (pos) { pos--; return true; } else return false; } template bool TStack ::top(mType& res) { if (pos) { res = data[pos-1]; return true; } else return false; } int main() { int res; TStack si; TStack sc; si << 10 << 20 << 30; si.top(res); std::cout << res; si.pop(); std::cout << si.size(); return 0; }