#include #include // Question I.1 1.5 points struct item_t { size_t weight; virtual void display() = 0; item_t(size_t weight) : weight { weight } { } }; // Question I.2 2.5 points struct sword_t : item_t { size_t damage; sword_t(size_t damage, size_t weight) : item_t { weight }, damage { damage } { } void display() { std::cout << "* sword { damage: " << damage << ", weight: " << weight << " }" << std::endl; } }; // Question II.1: 2.5 points template struct list_t { struct node_t { node_t* next; T* value; }; node_t* root; list_t() : root { nullptr } { } // Question II.2 1.5 point void push(T* value) { root = new node_t { root, value }; } // Question II.3 3 points T* pop() { if(root) { node_t* tmp = root; T* res = root->value; root = root->next; delete tmp; return res; } else return nullptr; } // Question IV.1 3 points struct iterator_t { node_t* cur; bool operator != (const iterator_t& it) { return cur != it.cur; } iterator_t& operator ++ () { cur = cur->next; return *this; } T* operator *() { return cur->value; } }; iterator_t begin() { return iterator_t { root }; } iterator_t end() { return iterator_t { nullptr }; } }; // // Exercise III: 6 points // struct inventory_t { // Question III.1 1 point list_t items; // Question III.2 1 point void add(item_t* item) { items.push(item); } // Question III.3 2 points size_t weight() { size_t total = 0; for(auto* cur=items.root; cur; cur=cur->next) total += cur->value->weight; return total; } // Question III.4 2 points void display() { std::cout << "inventory:" << std::endl; for(auto* cur=items.root; cur; cur=cur->next) cur->value->display(); std::cout << " => " << weight() << std::endl; } }; // not graded int main(int argc, char* argv[]) { inventory_t inventory; inventory.add(new sword_t { 25, 10 }); inventory.display(); for(auto* cur: inventory.items) cur->display(); }