complete h file cpp file is complete and an output is give
/**************************************************************************
* complete .h file (.cpp file is complete) and an output is given *
**************************************************************************/
implement a generic dynamic array class. It will be atemplated class that provides the basic features of an array.
/********************************************************************
* output: bold one\'s are the one\'s I didn\'t know how to do *
********************************************************************/
The correct output is shown below:
The capacity of D is 15
The capacity of E is 15
The capacity of D is now 25
The capacity of E is now 30
D[0] = 11
Array index is negative
Array index is too large
/************************
* .cpp complete file *
*************************/
#include <string>
 int doubleIt(dynarr<int> A)
 {   
 int newsize = 2*A.getCapacity();
 A.reserve(newsize);   
    return newsize;
 }
 int main()
 {   
 dynarr<int> D(15);
 std::cout << \"The capacity of D is \" << D.getCapacity() << std::endl;
 dynarr<int> E(D);
 std::cout << \"The capacity of E is \" << E.getCapacity() << std::endl;
 D.reserve(25);   
 std::cout << \"The capacity of D is now \" << D.getCapacity() << std::endl;   
 E.reserve(2*E.getCapacity());   
 std::cout << \"The capacity of E is now \" << E.getCapacity() << std::endl;
 D[0] = 11;   
 std::cout << \"D[0] = \" << D[0] << std::endl;
 try {   
 std::cout << D[-1] << std::endl;   
 }   
    catch(InvalidIndex &ex) {
    std::cout << ex.getMessage() << std::endl;
 }
 try {
    std::cout << D[30] << std::endl;
 }   
 catch(InvalidIndex &ex) {
    std::cout << ex.getMessage() << std::endl;
 }   
    return 0;
   
 }
/************************************
 * .h file (should be completed) *
 *************************************/
 #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) throw(InvalidIndex);   
 int getCapacity();
 void reserve(int newcap);
 };
 template <class dynElem>
 dynarr<dynElem>::dynarr(const dynarr<dynElem> &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()
 {
 
 
 }
 template <class dynElem>
 dynarr<dynElem> & dynarr<dynElem>::operator=( const dynarr<dynElem> &other)
 {
 dynarr temp (other);
 return temp;
 }   
 template <class dynElem>
 dynElem & dynarr<dynElem>::operator[](int ndx) throw(InvalidIndex)
 {
 //if(ndx < capacity)
 return A[ndx];
 /*else if(ndx < 0)
 std::cout<<\"Array index is negative\"<<std::endl;
 else
 std::cout<<\"Array index is too large\"<<std::endl; */
 }
 template <class dynElem>
 int dynarr<dynElem>::getCapacity()
 {   
 return capacity;
   
 }   
 template <class dynElem>
 void dynarr<dynElem>::reserve(int newcap)
 {
 // if newcap <= capacity, does nothing;
 if(newcap > capacity)
 {
 // if capacity is 0, allocates a dynamic array of   
 // capacity newcap and makes A point to that array;   
 if(capacity == 0)
 A = new dynElem[newcap];
 // otherwise allocates a new dynamic array newA of capacity   
 // newcap, copies values in A to newA, deletes A and sets   
 // A equal to newA
 else
 {
 dynElem *newA = new dynElem[newcap];
 for(int i = 0; i < capacity; i++)
 newA[i] = A[i];
 delete A;
 A = newA;
 capacity = newcap;
 }
 }
 }
Solution
The following the code for only .h file where the exception throwing is handled for negative and overlfown indexes
PROGRAM CODE:
#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) throw(InvalidIndex);   
 int getCapacity();
 void reserve(int newcap);
 };
 template <class dynElem>
 dynarr<dynElem>::dynarr(const dynarr<dynElem> &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()
 {
 delete A;
 }
 template <class dynElem>
 dynarr<dynElem> & dynarr<dynElem>::operator=( const dynarr<dynElem> &other)
 {
 dynarr temp (other);
 return temp;
 }   
 template <class dynElem>
 dynElem & dynarr<dynElem>::operator[](int ndx) throw(InvalidIndex)
 {
 if(ndx < 0)
 throw InvalidIndex(\"Array Index is negative\") ;
 else if(ndx < capacity)
 return A[ndx];
 else if(ndx > capacity)
 throw InvalidIndex(\"Array index is too large\");
 }
 template <class dynElem>
 int dynarr<dynElem>::getCapacity()
 {   
 return capacity;
   
 }   
 template <class dynElem>
 void dynarr<dynElem>::reserve(int newcap)
 {
 // if newcap <= capacity, does nothing;
 if(newcap > capacity)
 {
 // if capacity is 0, allocates a dynamic array of   
 // capacity newcap and makes A point to that array;   
 if(capacity == 0)
 A = new dynElem[newcap];
 // otherwise allocates a new dynamic array newA of capacity   
 // newcap, copies values in A to newA, deletes A and sets   
 // A equal to newA
 else
 {
 dynElem *newA = new dynElem[newcap];
 for(int i = 0; i < capacity; i++)
 newA[i] = A[i];
 delete A;
 A = newA;
 capacity = newcap;
 }
 }
 }
OUTPUT:





