Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a DynamicList template class using the IntList class has a starter code. Note: Template class definitions will solely reside in the specification ( .
Create a DynamicList template class using the IntList class has a starter code.
Note: Template class definitions will solely reside in the specification h file.
You must name your files DynamicList.h and excpp
File: IntList.cpp
#include "IntList.h
#include
IntList::IntListint cap
list new intcap;
capacity cap;
numElements ;
IntList::~IntList
delete list;
IntList::IntListconst IntList &obj
assign numElements and capacity from obj
capacity obj.capacity;
numElements obj.numElements;
allocate memory based on new capacity
list new intcapacity;
copy over elements from obj
for int i ; i numElements; i
listi obj.listi;
IntList& IntList::operatorconst IntList &obj
if this &obj
deallocate memory
delete list;
assign numElements and capacity from obj
capacity obj.capacity;
numElements obj.numElements;
allocate memory based on new capacity
list new intcapacity;
copy over elements from obj
for int i ; i numElements; i
listi obj.listi;
return this;
void IntList::addint el
if numElements capacity
resize;
listnumElements el;
numElements;
int IntList::getint el const
for int i ; i numElements; i
if listi el
return i;
return ;
bool IntList::empty const
return numElements ;
int IntList::size const
return numElements;
string IntList::tostring const
stringstream ss;
for int i ; i numElements; i
ss listi;
return ssstr;
void IntList::resize
update capacity
capacity ;
create new array based on updated capacity
int tempArr new intcapacity;
copy old array values to new array
for int i ; i numElements; i
tempArri listi;
delete old array
delete list;
reassign old array to new array
list tempArr;
File: IntList.h
#ifndef WEEKINTLISTH
#define WEEKINTLISTH
#include
using namespace std;
class IntList
public:
IntListint; Constructor
~IntList; Destructor
IntListconst IntList &; Copy constructor
IntList& operatorconst IntList &; Overloaded operator
void addint; add element to array
int getint const; find element in array; return index
where found or if not found
bool empty const; determines if list is empty or not
int size const; number of elements in array
string tostring const; returns string representation of
of the IntList class
private:
int list; Pointer to the array
int capacity; capacity of array
int numElements; Number of elements
void resize; resize array when full
;
#endif WEEKINTLISTH
File: main.cpp
#include "IntList.h
#include
using namespace std;
void printListStateconst IntList &;
void printIndexint int;
int main
const int SIZE ;
IntList numbersSIZE;
print List state
printListStatenumbers;
populate array
for int x ; x SIZE; x
numbers.addx;
print List state again
printListStatenumbers;
find value and
int val ;
int index numbers.getval;
printIndexval index;
index numbers.getSIZE;
printIndexSIZE index;
IntList numbersnumbers; calls the copy constructor
IntList numbersSIZE; calls constructor
numbers numbers; calls the overloaded operator
the destructor is automatically called when the IntList object
goes out of range; you cannot and should not call the destructor!
return ;
void printListStateconst IntList &obj
if objempty
cout "List is empty";
else
cout "List is not empty";
cout and has a size of obj.size endl;
void printIndexint val, int index
if index
cout val not found!" endl;
else
cout val found at index index endl;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started