Question

In C++ and comment so I UNDERSTAND Implement a class named DynamicArray that has the following...

In C++ and comment so I UNDERSTAND

Implement a class named DynamicArray that has the following members:
A pointer to hold a dynamically allocated array, of type int.
A member variable to hold the size of the array.
A default constructor, which will allocate an array of size 10
A parameterized constructor, which takes a size and use the size to allocate array.
A copy constructor, which performs deep copy.
A copy assignment operator, which performs deep copy and supports self-assignment of the form x = x. See reference: https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three
A destructor that recycles allocated memory
A member function that fills array with random numbers.
A member function that prints all elements from the array.
A member function that performs insertion sort.

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

//DynamicArray.h

class DynamicArray{
       private:
       int *data; // pointer variable
       int size; // member variable
   public:
       DynamicArray();
       DynamicArray(int size);
       DynamicArray(const DynamicArray &obj);
       DynamicArray& operator= (const DynamicArray &obj);
       ~DynamicArray(){ // Destructor
           delete [] data;
       }
       void insert();
       void print();
       void sort();
};

//DynamicArray.cpp

#include<iostream>
#include<cstdlib>
#include<time.h>
#include"DynamicArray.h"
using namespace std;

DynamicArray::DynamicArray(){ // default constructor
   size = 10;
   data = new int[size];
}
DynamicArray::DynamicArray(int size){ // parametrized constructor
   this->size = size;
   this->data = new int[this->size];
}
DynamicArray::DynamicArray(const DynamicArray &obj){ // copy constructor
   size = obj.size;
   data = new int[size];
   for(int i = 0 ; i < size; i++){
       data[i] = obj.data[i];
   }
}
DynamicArray& DynamicArray::operator= (const DynamicArray &obj){ // operator=
   if (this != &obj) {
data = new int[obj.size];
size = obj.size;
for (int i=0; i<size; i++) data[i] = obj.data[i];
}
return *this;
}

void DynamicArray::insert(){ // insert function
  
   for(int i = 0 ; i < this->size; i++){
       int d = rand()%100;
       this->data[i] = d;
   }
}
void DynamicArray::print(){ // print function
   for(int i = 0; i < this->size; i++){
       cout<<this->data[i]<<" ";
   }
   cout<<"\n";
}
void DynamicArray::sort(){ // insertionsort function
   int i,j,key;
   for(i = 1; i < size; i++){
       j = i-1;
       key = data[i];
       while(j >= 0 && data[j] > key){
           data[j+1] = data[j];
           j--;
       }
       data[j+1] = key;
   }
}
int main(){
   DynamicArray obj; // default
   srand(time(0)); // random number generation
   obj.insert();
   cout<<"Default Constructor: \n";
   cout<<"Before Sorting : "<<" ";
   obj.print();
   cout<<"After Sorting : "<<" ";
   obj.sort();
   obj.print();
   DynamicArray obj1(20);
   obj1.insert();
   cout<<"Parametrized Constructor: \n";
   cout<<"Before Sorting : "<<" ";
   obj1.print();
   cout<<"After Sorting : "<<" ";
   obj1.sort();
   obj1.print();
   cout<<"Copy Constructor: \n";
   DynamicArray obj3 = obj;
   obj3.print();
}

/* OUTPUT SCREEN */\

Default Constructor: Before Sorting : 84 56 2 40 62 71 87 80 57 25 After Sorting : 2 25 40 56 57 62 71 80 84 87 Parametrized

Add a comment
Know the answer?
Add Answer to:
In C++ and comment so I UNDERSTAND Implement a class named DynamicArray that has the following...
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
  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement...

    C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement the Inner and Outer classes (Circular Buffer of circular buffers using Queues). No need of any classes from the Standard Template Library (STL), not even vector. Add the member functions of those classes by following the codes of InnerCB.h and CBofCB.h below: // file: InnerCB.h // Header file for Inner Circular Buffer. // See project description for details. // #ifndef _INNERCB_H_ #define _INNERCB_H_ class...

  • please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits...

    please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits from File and is a non-abstract class. 1. Hence objects of the class JPG can be instantiated. 2. To do so, we must override the pure virtual function clone() in the base class. • The class declaration is given below. class JPG : public File { public: JPG(const std::string& n, int n, int w, const double rgb()) : File(...) { // ... } *JPG()...

  • Write a code in C++ by considering the following conditions :- Tasks :- 1. Create a...

    Write a code in C++ by considering the following conditions :- Tasks :- 1. Create a class Employee (with member variables: char * name, int id, and double age). 2. Create a constructor that should be able to take arguments and initialize the member variables. 3. Create a copy constructor for employee class to ensure deep copy. 4. Create a destructor and de allocate the dynamic memory. 5. Overload the assignment operator to prevent shallow copy and ensure deep copy....

  • 2. (50 marks) A string in C++ is simply an array of characters with the null...

    2. (50 marks) A string in C++ is simply an array of characters with the null character(\0) used to mark the end of the string. C++ provides a set of string handling function in <string.h> as well as I/O functions in <iostream>. With the addition of the STL (Standard Template Library), C++ now provides a string class. But for this assignment, you are to develop your own string class. This will give you a chance to develop and work with...

  • C++ computer science Given the partial class HardDrive implementation below, implement all of the following: 1:...

    C++ computer science Given the partial class HardDrive implementation below, implement all of the following: 1: default constructor that initialized the attributes with proper default data of your choice. 2: destructor() method, that releases all the dynamically allocated memory. 3: copy constructor() method: That properly manages the dynamic array when the object is passed by value to a function as a parameter. 4: overload the assignment operator: To properly handle copying the dynamic array when one object is assigned to...

  • In c++ show both .cpp and .hpp file for the class: class Task { private: //...

    In c++ show both .cpp and .hpp file for the class: class Task { private: // These three member variables store the basic information about a task string title; string location; // This pointer will point to a dynamically-allocated array // of THREE strings. Each element contains the name of a supply required for this task. // This should start out set to NULL. string * tags; // This integer stores the number of elements in the tags array. int...

  • Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in...

    Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in DynamicArray .h and DynamicArray.cpp files, according to the following UML class diagram: DynamicArray - int arrySize; - int currentSize; int* arrayPtr; + DynamicArray(int size) // Explicit constructor, which you define- allocate space in dynamic memory for an integer array of the given size. + DynamicArray) // Explicit destructor, which you define-de allocate dynamic memory. + additem(int item): bool // Set the value of the...

  • In c++ language Design and implement a Queue data structure using linked list. Support the following...

    In c++ language Design and implement a Queue data structure using linked list. Support the following usual operations: default constructor parameterized constructor to create a queue of user-specified capacity enqueue dequeue is_full is_empty display destructor that deallocates all the nodes copy constructor overloaded assignment operator Demonstrate using a main function.

  • In this assignment, you are asked to: 1. create a Matrix class that stores and operate...

    In this assignment, you are asked to: 1. create a Matrix class that stores and operate on a dynamic two-dimensional array. The class has the following structure: Private member variables: - int ** mat; - int rows; - int cols; Public member functions: +Default constructor: sets both rows and cols to 3 and creates 3x3 matrix dynamically +Parameterized constructor: sets the rows and cols to the values passed to the constructor and create a matrix with the given dimensions. +Destructor:...

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