C++ Class Templates
Mar 4, 2014 at 12:35am UTC
Hello, I wrote this code, and I am supposed to add template classes to it so we can put any data type (int and double)and the program would still work.
I've tried to added templates to List.cpp but I seem to get an error in my main function. In the main function you are supposed to add three numbers, print them and then delete them. What did I do wrong and how can I fix it?
Here is my original code:
http://pastebin.com/rrg5jwbB
Here is my templated code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#include <iostream>
using namespace std;
template <class T>
class List
{
public :
List();
List(int insize);
~List();
T get(int index) const ;
void add(int value);
T getSize() const ;
void remove(int index);
private :
T aindex;
T *listarray;
T size;
};
/*--------------------------Constructs and Destructs------------------------*/
template <class T>
List<T>::List()
{
size = 10;
aindex = 0;
listarray = new T[size];
}
template <class T>
List<T>::List(int insize):size(insize)
{
aindex = 0;
listarray = new T[insize];
}
template <class T>
List<T>::~List()
{
delete [] listarray;
size = 0;
}
/*--------------------------Get-------------------------*/
template <class T>
List<T>::getSize() const
{
return size;
}
/*----------------------Add your code here!!!! -------------*
/*---- add---*/
/* adds a value to array at end.
If array is too small, make bigger (size you need +10) new array copy
delete old array
*/
template <class T>
void List<T>::add(T value)
{
if (aindex < size)
{
listarray[aindex] = value;
aindex++;
}
else
{
T *temparray = new T[size+10];
for (int i = 0; i < size; i++)
{
temparray[i] = listarray[i];
}
size = size+10;
temparray[aindex] = value;
aindex++;
delete [] listarray;
listarray = temparray;
}
}
/*-----get---*/
/*return value at position given
*/
template <class T>
List<T>::get(T index) const
{
if ((index > -1) && (index < size))
{
return listarray[index];
}
else
{
return -1;
}
}
/*------remove----*/
/*Remove value at position
Just zero if at end.
If in middle, make zero and copy all above down to fill in slots
*/
template <class T>
void List<T>::remove(T index)
{
if (index == (aindex-1))
{
listarray[index] = 0;
aindex--;
}
else if (index < size)
{
listarray[index] = 0;
for (int i = index; i < (aindex-1); i++)
{
listarray[i] = listarray[i+1];
}
}
else
{
return ;
}
}
int main()
{
List *mylist = new List(1);
mylist->add(10);
mylist->add(15);
mylist->add(20);
cout << "Added three values\n" ;
cout << mylist->get(0) << " " << mylist->get(1) << " " << mylist->get(2) <<endl;
mylist->remove(1);
cout << mylist->get(0) << " " << mylist->get(1) << " " << mylist->get(2) <<endl;
}
Mar 4, 2014 at 12:59am UTC
Topic archived. No new replies allowed.