Data Structures, Using C++ Change Your classes to use template <class T> and del
ID: 3858192 • Letter: D
Question
Data Structures,
Using C++ Change Your classes to use template <class T> and delete typedef.
Change your ‘general list’ class to be a ‘template’ class. Run your queue and stack programs again to test your template class.
(Can somone help me change my data to use templates through out my program)
(Origonal CODE (C++))
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include <utility>
#include <iomanip>
using namespace std;
ofstream outfile;
const int CAPACITY = 20;
typedef int ET;
class List
{
public:
int pos;
int mysize;
ET myary[CAPACITY];
List()
{
mysize = 0;
pos = 0;
for (int i = 0; i < CAPACITY; i++)
{
myary[i] = 0;
}
}
List(List &l)
{
l.pos = 0;
for (int i = 0; i < l.mysize; i++)
{
myary[i] = l.getElement();
l.pos++;
}
mysize = l.mysize;
pos = l.pos;
}
bool empty()
{
return (mysize == 0);
}
void first()
{
if (mysize >= 0)
pos = 0;
}
void last()
{
if (mysize > 0)
pos = mysize - 1;
}
void prev()
{
if (pos > 0)
pos--;
}
void next()
{
if (pos + 1 < mysize)
pos++;
}
int getPos()
{
return pos;
}
void setPos(int n)
{
pos = n;
}
void insertBefore(ET v)
{
if (mysize == 0)
{
myary[0] = v;
pos = 0;
mysize = 1;
}
else
{
if (mysize + 1 > CAPACITY)
{
outfile << "The list has reached full capacity. ";
return;
}
else
{
for (int i = mysize; i >= pos; i--)
{
myary[i] = myary[i - 1];
}
myary[pos] = v;
mysize++;
}
}
}
void insertAfter(ET v)
{
if (mysize == 0)
{
myary[0] = v;
pos = 0;
mysize = 1;
}
else
{
if (mysize + 1 > CAPACITY)
{
outfile << "The list is full. ";
}
else
{
for (int i = mysize; i > pos; i--)
{
myary[i] = myary[i - 1];
}
myary[pos + 1] = v;
mysize++;
pos++;
}
}
}
ET getElement()
{
return myary[pos];
}
int size()
{
return mysize;
}
void replace(int n)
{
myary[pos] = n;
}
void erase()
{
for (int i = pos; i < mysize; i++)
{
myary[i] = myary[i + 1];
}
mysize--;
}
void clear()
{
for (int i = 0; i < mysize; i++)
{
myary[i] = 0;
}
mysize = 0;
pos = 0;
}
bool operator == (List& l)
{
if (l.size() != size())
return false;
else
{
for (int i = 0; i < size(); i++)
{
setPos(i);
l.setPos(i);
if (getElement() != l.getElement())
return false;
}
return true;
}
}
void operator = (List &l)
{
for (int i = 0; i < l.mysize; i++)
myary[i] = l.myary[i];
mysize = l.mysize;
pos = l.pos;
}
List operator + (List& l)
{
l.mysize = l.mysize + mysize;
return l;
}
friend ostream& operator << (ostream &out, List &l);
};
ostream& operator << (ostream &out, List &l)
{
for (int i = 0; i < l.size(); i++)
{
out << l.myary[i] << " ";
}
out << " ";
return out;
}
int main()
{
ofstream outfile;
outfile.open("ListClassPGM_Output.txt");
List a, b; int endit;
for (int i = 1; i <= 20; i++)
a.insertAfter(i);
outfile << "List a : " << endl;
outfile << " " << a << endl;
outfile << "Number of elements in a - " << a.size() << endl;
for (int i = 1; i <= 20; i++)
b.insertBefore(i);
outfile << "List b : " << endl;
outfile << " " << b << endl;
outfile << "Number of elements in b - " << b.size() << endl;
if (a == b)
outfile << "List a & b are equal" << endl;
else
outfile << "List a & b are Not equal" << endl;
a.first();
b.first();
outfile << "First elmenet in list a & b: " << a.getElement() << ", "
<< b.getElement() << endl;
for (int i = 0; i < a.size(); a.next(), i++);
for (int i = 0; i < b.size(); b.next(), i++);
outfile << "Last elmenet in list a & b: " << a.getElement() << ", "
<< b.getElement() << endl;
outfile << endl << endl << " Start of new stuff" << endl;
outfile << "a = " << a << endl;
outfile << "b = " << b << endl;
a.first();
b.first();
endit = a.size() / 2;
for (int i = 1; i<endit; i++)
{
a.next();
b.next();
}
a.last();
b.last();
a.insertAfter(100);
a.insertBefore(99);
outfile << " Test last " << endl;
outfile << "a = " << a << endl;
outfile << "b = " << b << endl;
outfile << "New position in Obj 'a' position = " << a.size() / 2 << endl;
for (int i = 1; i<8; i++)
{
a.erase();
b.replace(i);
}
outfile << "Modified Object 'a' " << endl;
outfile << "List a: " << a << endl;
List c(b);
outfile << "Copy Constructor c(b)" << endl;
outfile << "List b : " << b << endl;
outfile << "List c : " << c << endl;
if (c == b)
outfile << "List c & b are equal" << endl;
else
outfile << "List c & b are Not equal" << endl;
List e;
e = c;
outfile << "Object 'c' assigned to Object 'e':" << endl;
outfile << "List c : " << c << endl;
outfile << "List e : " << e << endl;
List d;
d = a;
d.first();
endit = d.size() / 2;
for (int i = 1; i <= endit; i++)
{
d.next();
d.erase();
}
outfile << "Results after some erases: Object d " << endl;
outfile << "List d : " << d << endl;
d.first();
endit = d.size();
for (int i = 1; i < endit; d.next(), i++)
{
d.insertBefore(d.getElement() * 2);
d.next();
}
outfile << "Results after some Replaces on d " << endl;
outfile << "List d : " << d << endl;
a.first();
endit = a.size();
for (int i = 1; i < endit; a.next(), i++)
{
a.insertBefore(a.getPos() + a.getElement());
a.next();
a.erase();
}
outfile << "Results after some weird stuff on list a" << endl;
outfile << "List a : " << a << endl;
List alist(b);
alist.clear();
for (int i = 1; i <= 10; i++)
alist.insertAfter(i);
alist.first();
outfile << "New List alist with positions above: " << endl;
for (int i = 1; i <= 10; i++) {
outfile << setw(5) << alist.getPos();
alist.next();
}
outfile << endl;
alist.first();
for (int i = 1; i <= 10; i++) {
outfile << setw(5) << alist.getElement();
alist.next();
}
outfile << endl;
List newa;
for (int i = 1; i <= 20; i++)
newa.insertAfter(i * 3);
outfile << "List alist and newa before swap " << endl;
outfile << " " << alist << endl;
outfile << " " << newa << endl;
outfile << endl << " check out boundary conditions" << endl;
List sq;
outfile << "number of elements in empty sq list = " << sq.size() << endl;
sq.first();
sq.erase();
outfile << "empty sq values " << sq << endl;
sq.insertBefore(999);
outfile << "sq values " << sq << endl;
sq.next(); sq.next();
outfile << "sq.getElement() = " << sq.getElement() << endl;
outfile << "sq values " << sq << endl;
outfile.close();
system("pause"); //This statement not needed in code blocks, just VS.
return 0;
}
Explanation / Answer
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include <utility>
#include <iomanip>
using namespace std;
ofstream outfile;
const int CAPACITY = 20;
//below line of code added to declare your class a template class. I am using ET as a template variable
//you can use any template variable like classical T
//Changes in program are commented
//NOTE
//if you want to make any class or function a template one then just add the below line
//template <class T, class U, ....> // i.e whatever the template parameter needed mention it in this line
//and after this define the whole class/function by treating T, U, etc as a type
//but whenever you declare the object then at that place you have to specify the actual type
template <class ET>
class List
{
public:
int pos;
int mysize;
ET myary[CAPACITY];
List()
{
mysize = 0;
pos = 0;
for (int i = 0; i < CAPACITY; i++)
{
myary[i] = 0;
}
}
List(List &l)
{
l.pos = 0;
for (int i = 0; i < l.mysize; i++)
{
myary[i] = l.getElement();
l.pos++;
}
mysize = l.mysize;
pos = l.pos;
}
bool empty()
{
return (mysize == 0);
}
void first()
{
if (mysize >= 0)
pos = 0;
}
void last()
{
if (mysize > 0)
pos = mysize - 1;
}
void prev()
{
if (pos > 0)
pos--;
}
void next()
{
if (pos + 1 < mysize)
pos++;
}
int getPos()
{
return pos;
}
void setPos(int n)
{
pos = n;
}
void insertBefore(ET v)
{
if (mysize == 0)
{
myary[0] = v;
pos = 0;
mysize = 1;
}
else
{
if (mysize + 1 > CAPACITY)
{
outfile << "The list has reached full capacity. ";
return;
}
else
{
for (int i = mysize; i >= pos; i--)
{
myary[i] = myary[i - 1];
}
myary[pos] = v;
mysize++;
}
}
}
void insertAfter(ET v)
{
if (mysize == 0)
{
myary[0] = v;
pos = 0;
mysize = 1;
}
else
{
if (mysize + 1 > CAPACITY)
{
outfile << "The list is full. ";
}
else
{
for (int i = mysize; i > pos; i--)
{
myary[i] = myary[i - 1];
}
myary[pos + 1] = v;
mysize++;
pos++;
}
}
}
ET getElement()
{
return myary[pos];
}
int size()
{
return mysize;
}
void replace(int n)
{
myary[pos] = n;
}
void erase()
{
for (int i = pos; i < mysize; i++)
{
myary[i] = myary[i + 1];
}
mysize--;
}
void clear()
{
for (int i = 0; i < mysize; i++)
{
myary[i] = 0;
}
mysize = 0;
pos = 0;
}
bool operator == (List& l)
{
if (l.size() != size())
return false;
else
{
for (int i = 0; i < size(); i++)
{
setPos(i);
l.setPos(i);
if (getElement() != l.getElement())
return false;
}
return true;
}
}
void operator = (List &l)
{
for (int i = 0; i < l.mysize; i++)
myary[i] = l.myary[i];
mysize = l.mysize;
pos = l.pos;
}
List operator + (List& l)
{
l.mysize = l.mysize + mysize;
return l;
}
//you have to also make a friend function template
//because it is not the part of class but it can access its member (property of friend function)
//or we can say you have to tell the whole world which is not aware of your class definition that
//you class is a template class
template<class T>
friend ostream& operator << (ostream &out, List<T> &l);
};
//Here you have to specify that below function is defined for a template class or it is taking a template variable
template<class T>
ostream& operator << (ostream &out, List<T> &l)
{
for (int i = 0; i < l.size(); i++)
{
out << l.myary[i] << " ";
}
out << " ";
return out;
}
//Now wherever you declare a class List object you have to specify its type like
//List<int> intList
//List<float> floatList
//etc
int main()
{
ofstream outfile;
outfile.open("ListClassPGM_Output.txt");
//Here you have to specify what is the type of your new template object in angle bracket, i used here int,
//you can use for other data type
List<int> a, b;
int endit;
for (int i = 1; i <= 20; i++)
a.insertAfter(i);
outfile << "List a : " << endl;
outfile << " " << a << endl;
outfile << "Number of elements in a - " << a.size() << endl;
for (int i = 1; i <= 20; i++)
b.insertBefore(i);
outfile << "List b : " << endl;
outfile << " " << b << endl;
outfile << "Number of elements in b - " << b.size() << endl;
if (a == b)
outfile << "List a & b are equal" << endl;
else
outfile << "List a & b are Not equal" << endl;
a.first();
b.first();
outfile << "First elmenet in list a & b: " << a.getElement() << ", "
<< b.getElement() << endl;
for (int i = 0; i < a.size(); a.next(), i++);
for (int i = 0; i < b.size(); b.next(), i++);
outfile << "Last elmenet in list a & b: " << a.getElement() << ", "
<< b.getElement() << endl;
outfile << endl << endl << " Start of new stuff" << endl;
outfile << "a = " << a << endl;
outfile << "b = " << b << endl;
a.first();
b.first();
endit = a.size() / 2;
for (int i = 1; i<endit; i++)
{
a.next();
b.next();
}
a.last();
b.last();
a.insertAfter(100);
a.insertBefore(99);
outfile << " Test last " << endl;
outfile << "a = " << a << endl;
outfile << "b = " << b << endl;
outfile << "New position in Obj 'a' position = " << a.size() / 2 << endl;
for (int i = 1; i<8; i++)
{
a.erase();
b.replace(i);
}
outfile << "Modified Object 'a' " << endl;
outfile << "List a: " << a << endl;
List<int> c(b);
outfile << "Copy Constructor c(b)" << endl;
outfile << "List b : " << b << endl;
outfile << "List c : " << c << endl;
if (c == b)
outfile << "List c & b are equal" << endl;
else
outfile << "List c & b are Not equal" << endl;
List<int> e;
e = c;
outfile << "Object 'c' assigned to Object 'e':" << endl;
outfile << "List c : " << c << endl;
outfile << "List e : " << e << endl;
List<int> d;
d = a;
d.first();
endit = d.size() / 2;
for (int i = 1; i <= endit; i++)
{
d.next();
d.erase();
}
outfile << "Results after some erases: Object d " << endl;
outfile << "List d : " << d << endl;
d.first();
endit = d.size();
for (int i = 1; i < endit; d.next(), i++)
{
d.insertBefore(d.getElement() * 2);
d.next();
}
outfile << "Results after some Replaces on d " << endl;
outfile << "List d : " << d << endl;
a.first();
endit = a.size();
for (int i = 1; i < endit; a.next(), i++)
{
a.insertBefore(a.getPos() + a.getElement());
a.next();
a.erase();
}
outfile << "Results after some weird stuff on list a" << endl;
outfile << "List a : " << a << endl;
List<int> alist(b);
alist.clear();
for (int i = 1; i <= 10; i++)
alist.insertAfter(i);
alist.first();
outfile << "New List alist with positions above: " << endl;
for (int i = 1; i <= 10; i++) {
outfile << setw(5) << alist.getPos();
alist.next();
}
outfile << endl;
alist.first();
for (int i = 1; i <= 10; i++) {
outfile << setw(5) << alist.getElement();
alist.next();
}
outfile << endl;
List<int> newa;
for (int i = 1; i <= 20; i++)
newa.insertAfter(i * 3);
outfile << "List alist and newa before swap " << endl;
outfile << " " << alist << endl;
outfile << " " << newa << endl;
outfile << endl << " check out boundary conditions" << endl;
List<int> sq;
outfile << "number of elements in empty sq list = " << sq.size() << endl;
sq.first();
sq.erase();
outfile << "empty sq values " << sq << endl;
sq.insertBefore(999);
outfile << "sq values " << sq << endl;
sq.next(); sq.next();
outfile << "sq.getElement() = " << sq.getElement() << endl;
outfile << "sq values " << sq << endl;
outfile.close();
system("pause"); //This statement not needed in code blocks, just VS.
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.