Answered step by step
Verified Expert Solution
Link Copied!

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 ex5.cpp
// File: IntList.cpp
#include "IntList.h"
#include
IntList::IntList(int cap){
list = new int[cap];
capacity = cap;
numElements =0;
}
IntList::~IntList(){
delete [] list;
}
IntList::IntList(const IntList &obj){
// assign numElements and capacity (from obj)
capacity = obj.capacity;
numElements = obj.numElements;
// allocate memory based on new capacity
list = new int[capacity];
// copy over elements (from obj)
for (int i =0; i < numElements; i++)
list[i]= obj.list[i];
}
IntList& IntList::operator=(const 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 int[capacity];
// copy over elements (from obj)
for (int i =0; i < numElements; i++)
list[i]= obj.list[i];
}
return *this;
}
void IntList::add(int el){
if (numElements >= capacity)
resize();
list[numElements]= el;
numElements++;
}
int IntList::get(int el) const {
for (int i =0; i < numElements; i++){
if (list[i]== el)
return i;
}
return -1;
}
bool IntList::empty() const {
return numElements ==0;
}
int IntList::size() const {
return numElements;
}
string IntList::to_string() const {
stringstream ss;
for (int i =0; i < numElements; i++)
ss << list[i]<<"";
return ss.str();
}
void IntList::resize(){
// update capacity
capacity *=2;
// create new array based on updated capacity
int * tempArr = new int[capacity];
// copy old array values to new array
for (int i =0; i < numElements; i++)
tempArr[i]= list[i];
// delete old array
delete [] list;
// reassign old array to new array
list = tempArr;
}
// File: IntList.h
#ifndef WEEK1_INTLIST_H
#define WEEK1_INTLIST_H
#include
using namespace std;
class IntList {
public:
IntList(int); // Constructor
~IntList(); // Destructor
IntList(const IntList &); // Copy constructor
IntList& operator=(const IntList &); // Overloaded = operator
void add(int); // add element to array
int get(int) const; // find element in array; return index
// where found or -1 if not found
bool empty() const; // determines if list is empty or not
int size() const; // number of elements in array
string to_string() 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 //WEEK1_INTLIST_H
// File: main.cpp
#include "IntList.h"
#include
using namespace std;
void printListState(const IntList &);
void printIndex(int, int);
int main(){
const int SIZE =5;
IntList numbers(SIZE);
// print List state
printListState(numbers);
// populate array
for (int x =1; x <= SIZE; x++)
numbers.add(x);
// print List state again
printListState(numbers);
// find value 5 and 20
int val =5;
int index = numbers.get(val);
printIndex(val, index);
index = numbers.get(SIZE);
printIndex(SIZE, index);
IntList numbers2(numbers); // calls the copy constructor
IntList numbers3(SIZE); // calls constructor
numbers3= 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 0;
}
void printListState(const IntList &obj){
if (obj.empty())
cout << "List is empty";
else
cout << "List is not empty";
cout <<" and has a size of "<< obj.size()<< endl;
}
void printIndex(int val, int index){
if (index ==-1)
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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions