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

Objects with Dynamically Allocated Members Variable Size Array with Classes, Tes

ID: 3692136 • Letter: O

Question

Objects with Dynamically Allocated Members

Variable Size Array with Classes, Testing.

Create a project titled Exercise_VarArrayTest. Implement the dynamically expanding and contracting integer array described in the previous lab as a class. You should use class definition show in vararray.h Attributes are a pointer to the dynamically allocated array dAarray and the array size sizeThis class contains two groups of methods:

Member functions output(), check() addNumber() and removeNumber() have exactly the same functionality as described below:

output() takes the pointer to the array and its size and prints the arrays' contents

check() takes the pointer to the array and a number and checks if the number is in the array. If the number is in the array, returns the index of the element of the array holding the number. Otherwise, returns -1.

addNumber() takes a pointer to the array and adds a number to the array if the number is not already there. The pointer to the array is passed by reference. Note that the array cannot be enlarged to accommodate the number. Instead, this function needs to allocate a new array whose size is one more than the size of the old array, copy the contents of the old array to the new one, include the new number, deallocate the old array and then assign the address of the new array back to the original pointer.

Note that since the pointer is passed by reference, the call to this function results in effectively "enlarging" the array pointed to by the pointer.

removeNumber() takes a pointer to the array, a number, and removes the number if it is present in the array. If the number is removed, the array shrinks.

copy constructor, overloaded assignment and destructor ensure correct handling of the objects with dynamically allocated members.

Your code should work with the test shown in //Figure 2:. It is designed to test your implementation of varArray class.

You are free to modify the class and the test program but you cannot change the principle of dynamic allocation of the array (i.e. you cannot make the member variables automatic) or remove testing of all the methods from the test program.

Variable Size Array with Classes, Implementation. Create a project titled exercise_VarArrayClasses. Using the class implemented in the first part of this lab, implement the functionality of the second part of the previous lab. That is, write a program that asks the user to input numbers to add to and remove from the array and then prints its contents, see example dialog below.

Here is an example program dialog:

vararray.h

// variable size array class

#ifndef VARARRAY_H_

#define VARARRAY_H_

class varArray{

public:

varArray(); // void constructor

int arraySize() const {return size;} // returns the size of the array

int check(int number); // returns index of element containg "number" or -1 if none

void addNumber(int);    // adds number to the array

void removeNumber(int); // deletes the number from the array

void output();      // prints the values of the array

// big three

varArray(const varArray&); // copy constructor

varArray& operator=(const varArray&); // overloaded assignment

~varArray(); // destructor

private:

int *dArray; // pointer to the dynamically allocated array

int size;   // array size

};

#endif /* VARARRAY_H_ */

//Figure 2:

#include <iostream>

#include "vararray.h"

using std::cout; using std::endl;

void testfunc(varArray); // function to test pass-by-value for varArray

int main(){

   varArray a1;

   // testing regular member functions

   a1.addNumber(1);

   a1.addNumber(2);

   a1.addNumber(3);

   a1.addNumber(3); // trying to add duplicate, should not add it

  

   cout << "a1 size is after number addition is: " << a1.arraySize() << endl;

   if(a1.check(1) != -1) // check() returns -1 if number not present

      cout << "1 is present in the array" << endl;

   if(a1.check(5) != -1)

      cout << "5 is present in the array" << endl;

   cout << "a1 before removal of 2: "; a1.output();

   a1.removeNumber(2);

   cout << "a1 after removal of 2: "; a1.output();

   // uncomment this when you debugged the first part

   /*

   testfunc(a1); // testing copy constructor

  

   cout << "a1 after testfunc call: ";

   a1.output(); // if destructor is implemented correctly

                // this should print properly after testfunc completes

   varArray a2,a3;

   a3=a2=a1; // testing stackability of the overloaded assignment

   cout << "a3 after stackable assingment: ";

   a3.output();

   a3=a3; // testing protection against self-assingment

   cout << "a3 after self-assignment: ";

   a3.output();

   */

}

/*

// tests pass-by-value for object of class varArray

void testfunc(varArray va){ // copy constructor is invoked on "va"

    cout << "parameter va: ";

    va.output();

} // destructor is invoked when "va" goes out of scope

*/

Explanation / Answer

using namespace std;

varArray::varArray(void){

{

size=0;

array=new int[size];

}

void varArray::addNumber(int number){

{

int *array2=new int[size+1];

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

array2[i]=array[i];

array2[size]=number;

delete [] array;

array=array2;

size++;

}

void varArray::removeNumber(int number){

int *array2=new int[size-1];

bool tF=false;

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

{

if(array[i]==number)

{

array[i]=array[size-1];

tF=true;

}

}

if(tF){

for(int j=0; j<size-1; j++)

array2[j]=array[j];

delete [] array;

array=array2;

size--;

}

}

void varArray::output(void)

{

int temp;

for(int i=0; i<size-1; i++)

{

for(int j=i; j<size; j++)

{

if(array[j]<array[i])

{

temp=array[i];

array[i]=array[j];

array[j]=temp;

}

}

}

cout << "your numbers in ascending order are: ";

for(int r=0; r<size; r++)

cout << array[r] << ',';

}

varArray::varArray(const varArray& va){

size=va.size;

int *array;

array=new int[size];

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

array[i]=va.array[i];

}

varArray varArray::operator=(const varArray& va)

{

if (this != & va)

{

size=va.size;

delete [] array;

array=new int[size];

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

array[i]=va.array[i];

}

return *this;

}

varArray::~varArray()

{

delete [] array;

}