In: Computer Science
Below is a class called pointerDataClass that store data in a one-dimensional array using pointer.
#include<iostream>
using namespace std;
class pointerDataClass
{ int maxSize;//variable to store the maximum size of p
int length;//variable to store the number of elements in p
int *p;// pointer to an int array
public:
//Constructor to create an array of the size specified by the
parameter size.
pointerDataClass(int size);
//Destructor to deallocate the memory space occupied by the array p
~pointerDataClass();
//the function insertAt inserts num into array p at the position
specified by
//index
void insertAt(int index, int num);
//The function displayData displays all the array elements in p
void displayData();
};
PLEASE DO THIS WITH C++
Also, using declaration of array and using [] notation for accessing element of array
CPP code:
#include<iostream>
using namespace std;
class pointerDataClass{
int maxSize;//variable to store the maximum size of
p
int length;//variable to store the number of elements
in p
int *p;// pointer to an int array
public:
//Constructor to create an array of the size specified
by the parameter size.
pointerDataClass(int size);
//Destructor to deallocate the memory space
occupied by the array p
~pointerDataClass();
//the function insertAt inserts num into array p at
the position specified by
//index
void insertAt(int index, int num);
//The function displayData displays all the array
elements in p
void displayData();
};
//Constructor to create an array of the size specified by the
parameter size.
pointerDataClass::pointerDataClass(int size){
// allocate dynamic memory for integer array of size
length
// and assigning it to pointer p
this->p=new int[size];
// setting maxSize of array as size
this->maxSize=size;
// initially setting the number of elements in array
to 0
this->length=0;
// initially setting all elements of array to
0
// to avoid gibberish data
for(int index=0; index<size; index++){
this->p[index]=0;
}
}
//Destructor to deallocate the memory space occupied by the
array p
pointerDataClass::~pointerDataClass(){
// deallocating/freeing dynamic memory of the
array
delete this->p;
}
//the function insertAt inserts num into array p at the position
specified by index
void pointerDataClass::insertAt(int index, int num){
// set value of array element (at location=index) to
num
this->p[index]=num;
// increase number of elements in array by 1
this->length++;
}
//The function displayData displays all the array elements in
p
void pointerDataClass::displayData(){
cout<<"Displaying all the elements in the array
(from start to end)"<<endl;
// iteratively display each element of array from
start to end
for(int index=0; index<this->maxSize;
index++){
cout<<this->p[index]<<' ';
}
cout<<endl;
}
// main function
int main(){
// Create a pointerDataClass object called list11 with
a size of 10 elements
pointerDataClass list11(10);
// inserting 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 to
array
for(int index=0; index<10; index++){
// at location=index, set value of
element=index
// example, at location 2, set
value of element=2
list11.insertAt(index,
index);
}
// Display the array p of the object list11
list11.displayData();
return 0;
}
Output: