In C write a class to represent a dynamically sized array of
In C++, write a class to represent a dynamically sized array of integers.Name it DynArray.
Class specifications:
Private data members:
data-a pointer to integer array data.
arrayize- an integer representing representing the array (allocated space).
datasize-an integer representing the amount of data stored in the array.
Public member functions: default constructor:
sets the array size to be 10 elements, the data size to be 0, and dynamically allocates the 10 element array space.
constructor receiving size: checks the size parameter value to make sure that it is a positive value. If the value is positive, it becomes the array size. If the value is not legal, the array size should be set to 0, and the dynamic allocation is performed.
copy constructor: receive one DynArray object as a parameter and make the new object a copy of that object with its own separately allocated memory space, but with the same sizes and same data. destructor: Dynamically de-allocate the array space and set the pointer value to NULL.
setArraySize: This function receives a size value If the parameter value is greater thatn the existing array size, change the array size, allocate a new array space, and deallocate the old array space. If the size value is invalid (smaller than current size), do nothing.
getArraySize: Return a copy of the array size field.
getDataSize:Return a copy of the data size field.
addValue: Receives an integer value. If there is currently room in the allocated array space, then store the value into the array. If there is not space, then relocate the array to another location with twice as much space as before, and then store the parameter value into the array.
= operator: Receives one DynArray object as a parameter and makes the current object a copy of that parameter object with its own separately allocated array space. Make sure to de-allocate the current array space before dynamically allocating a new one.
print: writes out all the data stored in the dynamic array.
Solution
PROGRAM CODE:
/*
* DynArray.cpp
*
* Created on: 31-Oct-2016
* Author: Kaju
*/
#include <iostream>
using namespace std;
class DynArray
{
public:
int *array; // array elements
int arraysize; // array size
int datasize; // data size
public:
//constructor for the class
DynArray()
{
arraysize = 10;
datasize = 0;
array = new int[arraysize];
}
//constructor based on
DynArray(int size)
{
if(size>0)
{
arraysize = size;
}
else
arraysize = 0;
datasize = 0;
array = new int[arraysize];
}
//copy constructor
DynArray(DynArray &dynarray)
{
arraysize = dynarray.arraysize;
datasize = dynarray.datasize;
array = new int[arraysize];
}
//destructor
~DynArray()
{
delete [] array;
}
//setting the arraysize
void setArraySize(int size)
{
if(size>arraysize)
{
arraysize = size;
array = new int[arraysize];
}
}
//getting array size value
int getArraySize()
{
return arraysize;
}
//getting the data size
int getDataSize()
{
return datasize;
}
//adding a value
int addValue(int value)
{
if(datasize == arraysize)
{
int *array1 = new int[arraysize*2];
array = array1;
datasize++;
}
else
{
array[datasize] = value;
datasize++;
}
return 1;
}
//= operator overloading
void operator =(const DynArray dyna)
{
arraysize = dyna.arraysize;
datasize = dyna.datasize;
delete [] array;
array = new int[arraysize];
}
void print()
{
for(int i=0; i<datasize; i++)
{
cout<<array[i]<<endl;
}
}
};
int main()
{
DynArray array(10);
array.addValue(25);
array.print();
return 0;
}
OUTPUT:
25



