Dynamic array in C++14 (g++ 6.2 or higher). Please make sure it compiles without
ID: 3878315 • Letter: D
Question
Dynamic array in C++14 (g++ 6.2 or higher). Please make sure it compiles without errors or warnings on a Linux system.
Here's an example of what flags I use to compile: g++ -g -Wall -Wextra -Wpedantic -c example.cpp
Thanks will rate!
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 will be given an incomplete file dynarr.h whose contents are shown above. You are to complete the code in that file and submit it. It is the only file you are to submit. Also, you should create a program to test your implementation. Make sure your test file exercises all the methods of the dynarray class.
#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>
using namespace std;
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) throw(InvalidIndex);
int getCapacity() const;
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)
{
int i = 0;
capacity = other.getCapacity();
A = new dynElem[capacity];
std::copy(other.A, other.A +capacity, A);
}
template <class dynElem>
dynarr<dynElem>::~dynarr()
{
capacity = 0;
delete[] A;
}
template <class dynElem>
dynarr<dynElem> & dynarr<dynElem>::operator=(const dynarr<dynElem> &other)
{
int i = 0;
if (this == &other)
return *this;
delete[] A;
A = new dynElem[other.getCapacity()];
capacity = other.getCapacity();
std::copy(other.A, other.A + capacity, A);
return *this;
}
template <class dynElem>
dynElem & dynarr<dynElem>::operator[](int ndx) throw(InvalidIndex)
{
if (ndx >= capacity)
throw InvalidIndex("Array Index Out of Bound");
return A[ndx];
}
template <class dynElem>
int dynarr<dynElem>::getCapacity() const
{
return capacity;
}
template <class dynElem>
void dynarr<dynElem>::reserve(int newcap)
{
if (newcap <= capacity) { //do nothing
}
else if (capacity == 0) {
A = new dynElem[newcap];
capacity = newcap;
}
else {
dynElem *newA = NULL;
newA = new dynElem[newcap];
std::copy(A, A+capacity, newA);
capacity = newcap;
delete[] A;
A = newA;
}
}
template <class dynElem>
void print(dynarr<dynElem> &obj1) {
int i = 0;
for (i = 0; i < obj1.getCapacity(); i++)
cout << obj1[i] << " ";
}
int main() {
dynarr<int> obj1(4);
print(obj1);
cout<<obj1.getCapacity()<<endl;
obj1.reserve(2);
dynarr<int> obj2(obj1);
print(obj2);
obj1.reserve(10);
print(obj1);
dynarr<int> obj3;
obj3 = obj1;
print(obj3);
obj2 = obj3;
print(obj2);
return 0;
}
Sample Output :
Few modifications were requried in original function declaration in class.
dynElem & operator[] (int ndx) throw(InvalidIndex);
int getCapacity() const;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.