Question

In: Computer Science

C++ Memory Management: - Create a class called DynamicArray that allocates an integer array of user...

C++ Memory Management:

- Create a class called DynamicArray that allocates an integer array of user defined size on the heap

- Don't not use any of the STL containers (vector, list, etc)

- Create the necessary constructors and destructor, code should allocate the array using new.

- Write a method print_array that prints the array’s length and the contents of the array.

- Create at least 2 DynamicArray objects and print their contents. Include your DynamicArray class below along with the constructors/destructor and sample output.

- Below is a sample main method that declares, fills (not required as part of the project, but it makes testing easier), and prints a DynamicArray

int main()

{

    DynamicArray a(9);

    a.fill_array(1); /* fills array with consecutive #’s */

    a.print_array();

}

- in the comments label code as 'constructor' and 'destructor'

-show full code to pls

Solutions

Expert Solution

#include <iostream>

using namespace std;

class DynamicArray {

// Access specifier

public:

//Data Members

int *data;

int n, size;

//Member Functions()

//Default constructor

DynamicArray() {

data = new int[8];

n = 8;

size = 0;

}

DynamicArray(int size) {

data = new int[size];

n = size;

size = 0;

}

//Copy constructor

DynamicArray(const DynamicArray &other) {

n = other.n;

data = new int[n];

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

data[i] = other.data[i];

}

}


void operator=(const DynamicArray &other){

n = other.n;

data = new int[n];

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

data[i] = other.data[i];

}

}

int countEntry(){

return size;

}

void fill_array(int k){

while(1) {

data[size++] = k;

if(size==n)

break;

}

}

void print_array(){

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

cout<< data[i] <<" ";

}

cout<<endl;

}

//Destructor

~DynamicArray() {

delete[] data;

}

};


int main(){

DynamicArray a(9);

a.fill_array(1);

a.print_array();

}




=================================================================

SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern. PLEASE UPVOTE


Related Solutions

C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
Fill in the blanks in the following C statement such that it allocates memory for an array of 50 character values:
(3 pts) Fill in the blanks in the following C statement such that it allocates memory for an array of 50 character values:char *name = (                ) malloc(                        );(3 pts) Write a declaration for an array a of 10 strings, each of which has at most 80 characters (including the null character):                                             __________________________________________(6 pts) Circle or underline all syntax, logic, and runtime errors found in the following C fragment. Be sure to circle omitted punctuation.char *name_ptr == NULL, name[50] = “Barack...
C# Language Create a class called evaluateValue that declares 3 integer class variables: zeroValue, positiveValue and...
C# Language Create a class called evaluateValue that declares 3 integer class variables: zeroValue, positiveValue and negativeValue. These should be declared as public and you should not use automatic properties to declare them. Your class should have a constructor that takes one integer argument. In the constructor you will code if statements to set one of the three class variables (indicators) to 1 if the number sent to the constructor is either equal to zero, negative, or positive. In the...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer array called nums that has 20 cells b. generate a random number between 5 and 30, and populate the array nums c. print the minimum and maximum number in the array nums d. print sum and average of numbers in the array nums Your output look like this: (Note: numbers shown below will be different in your program due to the random numbers) minimum...
Using C# Create a class called CreditAccount. When user create this account, she/he need to enter...
Using C# Create a class called CreditAccount. When user create this account, she/he need to enter sum of credit and loan repayment period in months. Monthly payment need to calculate using bank percentage. You must come up with a formula for calculating percentage, you can use any formula, you want.
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the...
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the user to think of a number between 0 and n−1, then makes guesses as to what the number is. After each guess, the program must ask the user if the number is lower, higher, or correct. You must implement the divide-and-conquer algorithm from class. In particular, you should round up when the middle of your range is in between two integers. (For example, if...
This assignment asks to create an array with 10 integer elements. Ask user to input 10...
This assignment asks to create an array with 10 integer elements. Ask user to input 10 integer values. going through all the array elements and find/print out all the prime numbers. Instructions: Only use for loops for this lab (no need to create methods or classes for this assignment). Put a comment on each line of your codes, explaining what each line of code does. basic java, please
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that defines an integer parameter to set the private integer attribute. Create a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumetException. Create a getter to return the private integer attribute value. Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public...
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT