Question

IN C++ Create a class to act as a generic array (i.e. the user will be...

IN C++ Create a class to act as a generic array (i.e. the user will be able to choose the data type to be stored by passing the appropriate template argument. Integer template arguments will also be used to set the upper and lower bounds of the array. Provide all necessary functionality to allow the class to act as an array ([] operator, = operator etc.). The array does not need to provide input or output methods to act on the entire array. Errors within operators and constructors will be handled with exceptions (try, throw, catch) Demonstrate that your array operators work inside a function if the array is passed in by reference as a “const”. Provide a Copy method to provide the same functionality as the = operator. Also, provide GetAt and SetAt methods to provide ways of getting and setting values at a particular index in the array.

what i have so far:

Array.h

#ifndef ARRAY_H
#define ARRAY_H

template <class T, int lower, int upper>   // the value used for lower, upper in main must be a constant
class Array
{
public:
   enum ErrorCodes { ChangingValues, DisplayingValues };
   Array();
   Array(const Array <T, lower, upper> &);
   ~Array();
   const T &   GetAt(int) const;
   void       SetAt(int, const T &);
   Array<T, lower, upper> & Copy(const Array<T, lower, upper> &);
   Array <T, lower, upper> &   operator =   (const Array <T, lower, upper> &);
   T &           operator []   (int);
   const T &   operator []   (int) const;
private:
   T   Data[upper - lower + 1];
};

template <class T, int lower, int upper>
inline const T & Array <T, lower, upper>::GetAt(int i) const
{
   return operator [] (i);
}

template <class T, int lower, int upper>
inline void Array <T, lower, upper>::SetAt(int i, const T & Value)
{
   (*this)[i] = Value;
}

template <class T, int lower, int upper>
Array <T, lower, upper>::Array()
{
}

template <class T, int lower, int upper>
Array <T, lower, upper>::Array(const Array <T, lower, upper> & A)
{
   int       i;

   for (i = 0; i < lower, upper; i++)
       Data[i] = A.Data[i];
}

template <class T, int lower, int upper>
Array <T, lower, upper>::~Array()
{
}

template <class T, int lower, int upper>
Array <T, lower, upper> & Array <T, lower, upper>::Copy (const Array <T, lower, upper> & A)
{
   int       i;

   for (i = 0; i < upper; i++)
       Data[i] = A.Data[i];
   return *this;
}

template <class T, int lower, int upper>
Array <T, lower, upper> & Array <T, lower, upper>::operator = (const Array <T, lower, upper> & A)
{
   int       i;

   for (i = 0; i < upper; i++)
       Data[i] = A.Data[i];
   return *this;
}

template <class T, int lower, int upper>
T & Array <T, lower, upper>::operator [] (int i)
{
   //       assert ((i >= 0) && (i < lower, upper));
   if ((i < lower) || (i >= upper))
   {
       delete[] & Array;
       throw ChangingValues;   // you can only throw one thing
   }
   else
       return Data[i];
}

template <class T, int lower, int upper>
const T & Array <T, lower, upper>::operator [] (int i) const
{
   //       assert ((i >= 0) && (i < lower, upper));
   if ((i < lower) || (i >= upper))
   {
       delete[] & Array;
       throw DisplayingValues;
   }
   else
       return Data[i];
}

#endif

><><><><><><>><><<><><><><><><<><><><<><><><>

main.cpp

#include <iostream>

using namespace std;

#include "Array.h"

void Func(const Array <int, 0, 10> &);

void main()
{
   try {
       Array <int, 0, 10>                   A1;
       Array <Array <int, 0, 10>, 0, 20>       A2D;
       char *                           pTemp;

       pTemp = new char[10];
       A1[3] = 99;
       Func(A1);
       delete[] pTemp;   // if a throw occurs with the [] operator, this delete will not be done (memory leak)
   }
   catch (Array <int, 0, 10>::ErrorCodes e)
   {
       switch (e)
       {
       case Array <int, 0, 10>::ChangingValues:
           cout << "Error in index when changing values" << endl;
           break;
       case Array <int, 0, 10>::DisplayingValues:
           cout << "Error in index when displaying values" << endl;
           break;
       default:
           cout << "Internal error 1 in system" << endl;
       }
   }
   cin.get();
}

void Func(const Array <int, 0, 10> & A)
{
   cout << A[13] << endl;
}

pretty sure my header file is right but when i go to test in main all i get is the error messages im throwing. how do i make it work and how do i display that everything is working and all the conditions are tested and correct?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#1fndef ARRAY H #define ARRAY H #include <assert.h> #include <memory.h> template <class T, int LB, int UB> class Array public

template <class T, int LB, int UB> inline Array <T, LB, UB> & Array <T, LB, UB>: :Copy (const Array <T, LB, UB> & A) return o

Num = NunE|ements - 1; Sorted = true; for (1 = 0; i < Num; i++) if (Data [i] Data [i 1]) = Data [i]; = Data [i + 1]; Temp Dat

template <class T, int LB, int UB> perator [1 (int i) const assert ((1 >= 0) && (i return Data [i - LB]; NumElements)); #endi

#ifndef ARRAY2 H #define ARRAY2 H namespace DynamicArray #include <assert.h> #include <memory.h» template <class T> class Arr

template <class T> inline T Array <T>: :At (int i) const return operator[] (i); template <class T> inline Array <T> & Array <

template <class T> Array <TArray() delete [] pData; template <class T> void Array <T>::Sort () int int bol Sorted Num; Temp;

#înclude Array . h void main () const int LB (-5) const int UB (15); int Array <int, 0, 15> Array <int, 1, i> Array <int, -

Add a comment
Know the answer?
Add Answer to:
IN C++ Create a class to act as a generic array (i.e. the user will be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Redesign your Array class from lab6 as a class template to work with the application below....

    Redesign your Array class from lab6 as a class template to work with the application below. write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below. #include "Array.h" main() {   Array<char> c(3);   c.setValue(0,'c');   c.setValue(1,'s');   c.setValue(2,'c');   cout << c;   Array<int> i(3);   i.setValue(0,1);   i.setValue(1,2);   i.setValue(2,5);   cout << i;   Array<int> j(3);   j.setValue(0,10);   j.setValue(1,20);   j.setValue(2,50);   cout << j;   Array<int> ij;   ij = i + j;   cout << ij;...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm....

    //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm. #include <iostream> using std::cout; using std::endl; template<class T> void sort(T a[], int numberUsed); //Precondition: numberUsed <= declared size of the array a. //The array elements a[0] through a[numberUsed - 1] have values. //The assignment and < operator work for values of type T. //Postcondition: The values of a[0] through a[numberUsed - 1] have //been rearranged so that a[0] <= a[1] <=... <= a[numberUsed -...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

    Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...

  • #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int...

    #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int size;    int used;    void doubleSize(); public:    MyArray();    ~MyArray();    int length();    void insertHead(Item i);    void insertTail(Item i);    void deleteHead();    void deleteTail();    void sortAscending();    void sortDescending();    Item operator [](int i){        return myarray[i];    } }; template <typename Item> MyArray<Item>::MyArray(){    size = 5;    used = 0;    myarray = new Item[size];...

  • In C++, develop a class that supports array rotation. Rotating an array is an operation where...

    In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT