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

USING C++: In this problem, you will be implementing an Array class, with some a

ID: 3823548 • Letter: U

Question

USING C++:

In this problem, you will be implementing an Array class, with some additional features, such as dynamic memory allocation and definingg the copy constructor and the assignment operator. Please refer to Stephen Prata, "C++ Primer" Chapter 12 for details
(a) The class must have the following data members:
-An int for the array size.
-A pointer to an int for the array elements.
(b) Memory management should be done dynamically using the appropriate new operator in the constructor and the delete operator in the destructor.
(c) The class must also de ne the following:
-A constructor
-A copy constructor
-Assignment operator
-Addition operator
-A method to sort the array
-A method to display the array elements
-Test the code for all of the above functionality.

Explanation / Answer

#include <iostream>
using namespace std;
class Memory{
   int n,arr[10];   //array of max size 10
   public:
   Memory(int n, int arr[10]){ //constuctor
   n=5;
   array[10]={2,1,3,0,5}; //defined array elements
   p=(double*)malloc(n*sizeof(double)); //dynamic allocation of memory
   }
   Memory(Memory &){ // copy constructor
   }
   ~Memory(){ //destructor
   free(p); // deallocate the memory
   }
   int sort(int arr[10]){ // method to sort the array
   int i,j,temp;
   for(i=0;i<5;i++){ // we used simple bubble sorting technique
       for(j=i+1;j<n;j++){
           if(a[i]>a[j]){
               temp=a[i];
               a[i]=a[j];
               a[j]=temp;
               }
           }
       }
   }
   void display(){ // method to display sorted array elements
   for(i=0;i<5;i++){
   cout<<"elements in the array are: " << arr[i];
   }}
}
int main{
   Memory m1,m2; //object creation
   m1.sort();
   m1.display();
   m1 = m2; // assignment operator
   return 0;
}