/* * slist.cpp * * Created on: 28.11.2013 * Author: trifon */ #include "linked_list.cpp" // SList // LinkedList > - не става class SElement { public: virtual void print() const = 0; virtual void collectNumbers(LinkedList&) const = 0; virtual ~SElement() {} }; // !!!typedef LinkedList SList; class SList : public LinkedList, public SElement { public: void print() const { cout << '('; for(I it = begin(); it; ++it) { (*it)->print(); cout << ' '; } cout << ')'; } void collectNumbers(LinkedList& l) const { for(I it = begin(); it; ++it) (*it)->collectNumbers(l); } ~SList() { cout << "Destroying list!\n"; for(I it = begin(); it; ++it) delete *it; } }; class SInteger : public SElement { private: int x; public: // позволява ни да пишем // SInteger i = 4; <-> SInteger i(4); SInteger(int _x = 0) : x(_x) {} // позволява ни да пишем // int y = i; operator int() const { return x; } void print() const { cout << x; } void collectNumbers(LinkedList& l) const { l.toEnd(x); } };