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

C++ program that consists of header and source files IntArray.h and IntArray.cpp

ID: 3737201 • Letter: C

Question

C++ program that consists of header and source files IntArray.h and IntArray.cpp which implement the class IntArray. The program is both questions 12.4 and 12.5 also has to have its own unit tests. I can't use the "error.h" functions instead throw an appropriate std::exception.  

Design and implement a class called Intarray that implements the following methods: 4. . A constructor Intarray (n) that creates an Intarray object with n elements, each of which is initialized to 0. A destructor that frees any heap storage allocated by the IntArray. A method size0 that returns the number of elements in the IntArray. A method get (k) that returns the element at position k. If k is outside the vector bounds, get should call error with an appropriate message. .A method put (k, value) that assigns value to the element at position x. As with get, the put method should call error if k is out of bounds. Your solution should be split into separate interface and implementation files in a manner similar to the charstack example from this chapter. In the initial version of the code, you should add the necessary definitions to the intarray.h file to prevent clients from copying IntArray objects. Design and implement a unit test to check the methods exported by the class. By keeping track of the array size and checking that index values are inside the array bounds, this simple IntArray class already fixes two of the most serious shortcomings of the built-in array type You can make the IntArray class from the preceding exercise look a little more like traditional arrays by overriding the bracket-selection operator, which has the following prototype: int & operator [1 (int k); Like the get and put methods, your implementation of operator [1 should check to make sure that the index k is valid. If it is, the operator I1 method should return the element by reference so that clients can assign a new value to a selection expression

Explanation / Answer

4)

Array.h

#include <iostream>

#include <new>

#include <system_error>

using namespace std;

class     IntArray{

            public:

            int                    *arr;

            int capacity;

            int size=0;

            IntArray(n);

           

            int size();

           

            int        get(int k);

           

            void put(int k, int value);

};

ArrayInt.cpp

#include <Array>

            IntArray::IntArray(n){

                        capacity = n;

                       

                        arr = new (nothrow) int[n];

                       

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

                                    arr[i] = 0;

                        }

            }

           

            IntArray:: size(){

                        return size;

            }

           

            IntArray:: get(int k){

                        try {

                                    int num = arr[k];

                        } catch (const system_error& e) {

                                    cout<<"Error Code "<<e.code<<" Description "<<e.what()<<endl;

                        }

                                    return num;

            }

           

            IntArray:: put(int k, int value){

                        try {

                                    arr[k] = value;

                                    size++;

                        } catch (const system_error& e) {

                                    cout<<"Error Code "<<e.code<<" Description "<<e.what()<<endl;

                        }

           

            }

5) Implementation of operator will remain same s get(), just update the signature of the function.

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