Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert the following class to a template class that can store a collection of a

ID: 3843055 • Letter: C

Question

Convert the following class to a template class that can store a collection of any datatype (no implentation needed):

// words1.h - version 1 of class Words

#include

#ifndef WORDS1_H

#define WORDS1_H

namespace lab03_1

{

class Words

{

public:

Words();

  

void append(std::string word);

std::string& operator[] (unsigned int index);

  

unsigned int size() const;

unsigned int get_capacity() const;

std::string operator[] (unsigned int index) const;

  

private:

std::string data[10]; // max words is 10 in ver. 1

unsigned int used;

unsigned int capacity;

};

}

#endif

*******************************

b) Rewrite the Words class from previous to support a forward iterator. You must include a new iterator data type that is typedef-ed to 'iterator'. Provide an inline implementation of the begin() and end() methods that return iterators to the first element of the Words class and one past the last element, respectively. You must also write a new forward iterator class with inline implementation of all methods that are supposed to be supported by forward iterators.

*******************************

c) Write code that uses your template Words class with a forward iterator(from previous). Your code should declare an object of type Words named intBag to store a collection of integers(instead of strings). Store three integers into 'intBag' using the append() method. Declare and initialize an iterator to intBag and write a loop to print all of the elements of the collection using the iterator.

Explanation / Answer

struct MyType
{
template<class T> void foo() { }
};
template<class U>
struct S
{
template<class T>
void bar()
{
MyType mt;
mt.template foo<int>();
mt.foo<int>();
T t;
t.template foo<int>();
t.foo<int>();
S<T> st;
st.template foo<int>();
st.foo<int>();
S<MyType> s;
s.bar<int>();
s.template bar<int>();
}
};