// pair.cpp - demonstrate class templates // Example in Murray, C++ Strategies and Tactics, // Addison-Wesley, 1993 // class pair packs a two objects into a container // for transportation. Can help a function return // more than one object. template class pair { public: pair(const L& larg, const R& rarg) : lft(larg), rgt(rarg) { } L left() const { return lft; } R right() const { return rgt; } void left(const L& larg) { lft = larg; } void right(const R& rarg) { rgt = rarg; } private: L lft; R rgt; } pair funct(int x, double y) { pair temp(2*x,x+y); return temp; } #include void main() { cout << "Demonstrating Template Classes - pair\n" << "==========================================\n\n"; int x = 3; double y = 5.555; // pair myPair; compiler won't generate default // ctor because another ctor was // defined. See Stroustrup, pg 570 // myPair = funct(x,y); So this doesn't work cout << " funct returned myPair(" << funct(x,y).left() << ", " << funct(x,y).right() << ")" << endl<< endl; }