Docsity

Homework for Solution - Object-Oriented Software | CS 2704, Assignments of Computer Science

Toggle sidebar
bg1
Homework Four Solutions
(1) template <class T, class FilterObj>
void filter(list<T>& l, FilterObj f) {
list<T>::iterator l_i = l.begin(); //to move through list
list<T>::iterator del; // point to item to delete
while (l_i != l.end()) {
del = l_i++; //del lags behind
if (f(*del)) l.erase(del); //remove if predicate true
}
}
(2) Instead of giving the specific answers, here’s the working code for the problem. The
answers for (a) and (b) are marked with comments. For part (c), the key is that you are
able to convert an ItemBase pointer to a Item<T> pointer, and then that you can extract
the data from the Item<T> object. The conversion is most easily done with a dynamic
cast. The only decision you have is how to deal with the situation where the type you are
asking for is not the right type; here, I throw an exception. The extraction from an
Item<T> object requires that the Item<T> class has an accessor. This accessor cannot be
a function of ItemBase, because the base class doesn’t know the type of the objects being
held. With these figured out you can write another templatized function for the
extraction. The only trick being that you have to recognize the limitations of the
compiler in figuring out the type for T, and make sure that the type occurs in the
parameter list. So, I used “void extract(T)” when I would rather have used “T extract()”.
#include <iostream>
#include <string>
#include <list>
/* Working program for hw#4 question 2.
This program compiles and runs using the egcs-1.1.2 compiler under
linux. This version of the compiler doesn’t require namespace
business.
*/
/*
class ItemBase
Used in polyList construction as base class for arbitrary
objects. An abstract class.
*/
class ItemBase {
public:
virtual void print() = 0;
virtual ~ItemBase() {}
};
pf3
pf4

Partial preview of the text

Download Homework for Solution - Object-Oriented Software | CS 2704 and more Assignments Computer Science in PDF only on Docsity!

Homework Four Solutions

template <class T, class FilterObj>

void filter(list& l, FilterObj f) {

list::iterator l_i = l.begin(); //to move through list

list::iterator del; // point to item to delete

while (l_i != l.end()) {

del = l_i++; //del lags behind

if (f(*del)) l.erase(del); //remove if predicate true

(2) Instead of giving the specific answers, here’s the working code for the problem. The

answers for (a) and (b) are marked with comments. For part (c), the key is that you are

able to convert an ItemBase pointer to a Item pointer, and then that you can extract

the data from the Item object. The conversion is most easily done with a dynamic

cast. The only decision you have is how to deal with the situation where the type you are

asking for is not the right type; here, I throw an exception. The extraction from an

Item object requires that the Item class has an accessor. This accessor cannot be

a function of ItemBase, because the base class doesn’t know the type of the objects being

held. With these figured out you can write another templatized function for the

extraction. The only trick being that you have to recognize the limitations of the

compiler in figuring out the type for T, and make sure that the type occurs in the

parameter list. So, I used “void extract(T)” when I would rather have used “T extract()”.

#include #include #include

/* Working program for hw#4 question 2.

This program compiles and runs using the egcs-1.1.2 compiler under linux. This version of the compiler doesn’t require namespace business. */

class ItemBase

Used in polyList construction as base class for arbitrary objects. An abstract class. */ class ItemBase { public: virtual void print() = 0; virtual ~ItemBase() {} };

class Item

Templatized class used to hold arbitrary data objects. Simply holds a T object. The only derived class of ItemBase. Implements the print() method, and also provides an accessor that is not available in the ItemBase class. */ template class Item : public ItemBase { public: Item () {} Item (const T& t) : info(t) {} Item (const Item& i) : info(i.info) {} void print() { cout << info; } T getData() { return info; } private: T info; };

class polyList

Heterogeneous list. Implemented as STL list of ItemBase pointers. Use of pointers necessary to avoid loss of data when each Item is cast to ItemBase. Includes templatized functions for inserting and extracting value from the end of the list.

class polyList { public: template void insert(const T&); // part b template void extract(T&); bool isEmpty(); void print(); ~polyList(); class WrongType {}; private: list<ItemBase*> lst; // part a };

polyList::insert

Templatized function to allow insertions of arbitrary types into lst. Creates an Item object and pushes its pointer at the end of lst.

/ template void polyList::insert(const T& t) { lst.push_back(static_cast<ItemBase>(new Item(t)));

polyList::~polyList

Destructor for polyList class. Iterates through list to delete objects. Necessary because list holds pointers. Requires virtual destructor in ItemBase. / polyList::~polyList() { list<ItemBase>::iterator l_i = lst.begin(); ItemBase* d; while (l_i != lst.end()) { d = *l_i++; delete d; } }

void main() { polyList l; l.insert(1); l.insert(string("two")); //this is a difference from example code l.print(); string s; try { if (!l.isEmpty()) l.extract(s); } catch (polyList::WrongType) { cout << "type error" << endl; } l.print(); int i; try { if (!l.isEmpty()) l.extract(i); cout << "i: " << i << endl; } catch (polyList::WrongType) { cout << "type error" << endl; } l.print();