*Autor: Wojciech Domski* *Markdown flavoured text. Use any Markdown editor to get preview.* # Szablony Implementacja klasy Bag oparta na szablonie. ``` #ifndef BAG_HH #define BAG_HH #include template class Bag { T value; public: T & set(){ return value; } const T get() const{ return value; } Bag operator + ( const Bag & arg) const{ Bag tmp; tmp.set() = value + arg.get(); return tmp; } }; template <> Bag Bag::operator + ( const Bag & arg) const{ Bag tmp; tmp.set() = value + arg.get() + 1.1; return tmp; } template std::ostream & operator << ( std::ostream & stream, const T & arg) { return stream << arg.get(); } #endif ``` Wykorzystanie klasy Bag dla typów _int_ i _double_. ``` #include #include "bag.hh" using namespace std; int main( void) { Bag i1, i2, i3; Bag d1, d2, d3; i1.set() = 10; i2.set() = 11; i3 = i1 + i2; cout << i3 << endl; d1.set() = 10; d2.set() = 11; d3 = d1 + d2; cout << d3 << endl; return 0; } ```