Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this project you are to implement a generic dynamic array class. It will be

ID: 3883696 • Letter: F

Question

For this project you are to implement a generic dynamic array class. It will be a templated class that provides the basic features of an array. You are to complete this and you should create a program to test your implementation. Make sure your test file exercises all the methods of the dynarray class.

dynarr.h

#include <cassert>
#include <iostream>
#include <string>

class RuntimeException // generic run-time exception
{
protected:
std::string errorMsg;
public:
RuntimeException(const std::string& err) { errorMsg = err; }
std::string getMessage() const { return errorMsg; }
};

class InvalidIndex : public RuntimeException
{
public:
InvalidIndex(const std::string& err): RuntimeException(err) {};
};

template <class dynElem>
class dynarr {
private:
    int capacity;
    dynElem *A;
public:
    dynarr() : capacity(0), A(NULL) {};
    dynarr(int N): capacity(N), A(new dynElem[N]){}
    dynarr(const dynarr<dynElem> &other);
    ~dynarr();
    dynarr<dynElem> & operator=( const dynarr<dynElem> &other);
    dynElem & operator[](int ndx);
    int getCapacity();
    void reserve(int newcap);
    // if newcap <= capacity, does nothing;
    // if capacity is 0, allocates a dynamic array of
    // capacity newcap and makes A point to that array;
    // otherwise allocates a new dynamic array newA of capacity
   // newcap, copies values in A to newA, deletes A and sets
    // A equal to newA

};

template <class dynElem>
dynarr<dynElem>::dynarr(const dynarr<dynElem> &other)
{

}

template <class dynElem>
dynarr<dynElem>::~dynarr()
{

}

template <class dynElem>
dynarr<dynElem> & dynarr<dynElem>::operator=( const dynarr<dynElem> &other)
{

}

template <class dynElem>
dynElem & dynarr<dynElem>::operator[](int ndx) throw(InvalidIndex)
{

}

template <class dynElem>
int dynarr<dynElem>::getCapacity()
{

}

template <class dynElem>
void dynarr<dynElem>::reserve(int newcap)
{

}

Explanation / Answer

#include <cassert>

#include <iostream>

#include <string>

class RuntimeException { // generic run-time exception

protected:

std::string errorMsg;

public:

RuntimeException(const std::string& err) { errorMsg = err; }

std::string getMessage() const { return errorMsg; }

};

class InvalidIndex : public RuntimeException {

public:

InvalidIndex(const std::string& err) : RuntimeException(err) {};

};

template <class dynElem>

class dynarr {

private:

int capacity;

dynElem *A;

public:

dynarr() : capacity(0), A(NULL) {};

dynarr(int N) : capacity(N), A(new dynElem[N]) {}

dynarr(const dynarr<dynElem> &other);

~dynarr();

dynarr<dynElem> & operator=(const dynarr<dynElem> &other);

dynElem & operator[](int ndx);

int getCapacity();

void reserve(int newcap);

// if newcap <= capacity, does nothing;

// if capacity is 0, allocates a dynamic array of

// capacity newcap and makes A point to that array;

// otherwise allocates a new dynamic array newA of capacity

// newcap, copies values in A to newA, deletes A and sets

// A equal to newA

};

template <class dynElem>

dynarr<dynElem>::dynarr(const dynarr<dynElem> &other) {

// code goes here

//this is a copy constructor which make itself a duplicate of other

capacity = other.capacity;

A = new dynElem[capacity];

for (int i = 0; i < capacity; ++i) {

A[i] = other.A[i];

}

}

template <class dynElem>

dynarr<dynElem>::~dynarr() {

// code goes here

//this is a destructor

//so in this part deallocate any dynamically allocated memory

//only dynamically allocated memory is array

//so free it

delete[] A;

}

template <class dynElem>

dynarr<dynElem> & dynarr<dynElem>::operator=(const dynarr<dynElem> &other) {

// code goes here

//basically this operator overloadding create a new class which is coppied from other

//if we are copying same class to itself then no need of any work

if (this == &other) {

return *this;

}

/*otherwise free any type of memory holded by our class array A and copy content of new one*/

/* delete old memory since it is not needed and assign new memory */

delete[] A;

A = new dynElem[capacity];

for (size_t i = 0; i < capacity; i++)

{

A[i] = other.A[i];

}

return *this;

}

template <class dynElem>

dynElem & dynarr<dynElem>::operator[](int ndx) throw(InvalidIndex) {

// code goes here

if (ndx >= capacity) {

throw InvalidIndex("Index is out of range");

}

else

{

return A[ndx];

}

}

template <class dynElem>

int dynarr<dynElem>::getCapacity() {

// code goes here

return capacity;

}

template <class dynElem>

void dynarr<dynElem>::reserve(int newcap) {

// code goes here

if (newcap > capacity) {

if (capacity == 0) {

A = new dynElem[newcap];

capacity = newcap;

}

else

{

dynElem *newA = new dynElem[newcap];

for (size_t i = 0; i < capacity; i++)

{

newA[i] = A[i];

}

delete[] A;

A = newA;

capacity = newcap;

}

}

}

/*main to test our program*/

int main() {

dynarr<int> coll;

std::cout << "Capacity of our coll is : " << coll.getCapacity() << " ";

coll.reserve(10);

std::cout << "Capacity of our coll after reserving it's size is : " << coll.getCapacity() << " ";

std::cout << "array initializing.... ";

for (size_t i = 0; i < 10; i++)

{

coll[i] = i;

}

dynarr<int> arr(coll);

std::cout << "content of array arr after copying it from coll ";

for (size_t i = 0; i < 10; i++)

{

std::cout << arr[i] << " ";

}

std::cout << std::endl;

std::cout << "Applying assignement ";

arr = arr;

std::cout << "content of array arr after assigning it to itself ";

for (size_t i = 0; i < 10; i++)

{

std::cout << arr[i] << " ";

}

std::cout << std::endl;

//this will throw error as index is out of range

//std::cout << coll[11] << std::endl;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote