C++代写 | CS11440 Lab 6 – Class Templates


Templates in C++ is not a difficult idea. This lab is not intended to help you understand the details of how templates work. It is only intented to give students an opportunity to start developing code using templated classes.

Everyone in the class has had to come to grips with the usage of variables in general. With templates, you can think of the type of the variable being a variable itself. The programmer is just saying that when you want to instantiate an instance of this class you will need to tell the compiler which variable type the class needs to use. After understanding that, templates just become a syntactical change.

Developing a templated class is not difficult either. I think the syntax is the most difficult aspect. It’s just pure ugly. So, I suggest that you approach the MyArray class as if it was the I nt Array class. Yes, I’m suggesting that you write pushBack, popBack, getSize, isEmpty, operator[], and toString as if you were writing the IntArray class we developed in the Big 4 lab. After completing that implementation and testing for correctness, go back and change all necessary instances of int to the template variable.

Here’s the MyArray class written as Mylnt from the Big 4 lab.

// You won * t be able to inherit from ContainerIfc at this point which means you may

// have to test locally to begin with.

class MyArray (

int *data;

int capacity, size;

// You will need to write all function defintions outside of the class defintion MyArray();


this->capacity = 5;

this->size = 0;

this->data = new int[this->capacity];

Here’s the MyArray class with the inheritance and templating in place.

template <class T>

class MyArray : public Containerlfc<T> {

int capacity, size;


// notice the T used in the name of the class not the name of the function

template <class T>


this->capacity = 5;

this->size = 0;

this->data = new T[this->capacity];

MyArray Class

MyArray will inherit from Containerlfc. Containerlfc is an abstract classes which requires you to implement the functions in your MyArray. In addition to overriding the pure virtual functions, you will need to implement the big four because of the precense of dynamic memory in the class.