please use the c++, finish the code CS 23001 Spring 2017 Page 4 of 5 M. Decker (
ID: 3588867 • Letter: P
Question
please use the c++, finish the code
CS 23001 Spring 2017 Page 4 of 5 M. Decker (25pts) Write a class for the ADT darray. You must use a dynamic array of int. You must implement the 4 methods below (that are not already implemented). Each should behave as one would expect in C++. The assignment must implement standard copy semantics. 6. //CLASS INV: data->int [ cap] && cap >= 0 class darray f private: int * data; int cap; public: darray darray darray darray() operator= () : data(0), cap (O) t/I darray of capacity o. (int); (const darray&); //1) darray of capacity n. 1/2) Copy Constructor //3) Destructor //4) Assignment darray& (darray);Explanation / Answer
Here is the class implemented for you:
//CLASS INV: data->int[cap] && cap >= 0
class darray
{
private:
int* data; //The pointer to hold the array.
int cap; //The capacity of the array.
public:
darray() : data(0), cap(0) {} //darray of capacity 0.
darray(int); //darray of capacity n.
darray(const darray&); //Copy constructor.
~darray ();
darray& operator= (darray);
};
darray::darray(int capacity)
{
cap = capacity;
data = new int[cap];
}
darray::darray(const darray& other)
{
cap = other.cap;
data = new int[cap];
for(int i = 0; i < cap; i++)
data[i] = *(other.data + i);
}
darray::~darray()
{
delete data;
cap = 0;
}
darray& darray::operator=(darray other)
{
cap = other.cap;
data = new int[cap];
for(int i = 0; i < cap; i++)
data[i] = *(other.data + i);
return *this;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.