In: Computer Science
**** IN C++ ****
1) Modify the class pointerDataClass so the main function below is working properly. Use deep copy.
int main()
{
pointerDataClass list1(10);
list1.insertAt(0, 50);
list1.insertAt(4, 30);
list1.insertAt(8, 60);
cout<<"List1: " < list1.displayData();
cout<<"List 2: "< pointerDataClass list2(list1);
list2.displayData();
list1.insertAt(4,100);
cout<<"List1: (after insert 100 at indext 4) " <
list1.displayData();
cout<<"List 2: "< list2.displayData();
return 0;
}
Code:
#include
using namespace std;
class pointerDataClass
{
int maxSize;
int length;
int *p;
public:
pointerDataClass(int size);
~pointerDataClass();
void insertAt(int index, int num);
void displayData();
};
pointerDataClass::pointerDataClass(int size)
{
maxSize = size;
p=new int[maxSize];
length = 0;
}
pointerDataClass::~pointerDataClass()
{
delete[] p;
}
void pointerDataClass::insertAt(int index, int num)
{
for(int i=length-1; i>=index; i--)
{
p[i] = p[i-1];
}
p[index]=num;
length++;
}
void pointerDataClass::displayData()
{
for(int i=0;i {
cout< }
cout< }
int main()
{
pointerDataClass list11(10);
for (int i =0;i<10;i++)
{
list11.insertAt(i, i);
}
list11.displayData();
return 0;
}
SOLUTION-
I have solve the problem in C++ code with comments and output
screenshot for easy understanding :)
CODE-
#include<iostream>
using namespace std;
class pointerDataClass
{
int maxSize;
int length;
int *p;
public:
pointerDataClass(int size);
~pointerDataClass();
pointerDataClass(const
pointerDataClass&other);
void insertAt(int index, int num);
void displayData();
void get();//get the length of the arrray
};
pointerDataClass::pointerDataClass(int size)
{
this->p = new int[size];
this->maxSize = size;
this->length = 0;
for (int index = 0; index < size; index++)
this->p[index] = 0;
}
pointerDataClass::~pointerDataClass()
{
delete this->p;
}
pointerDataClass::pointerDataClass(const
pointerDataClass&other)
{
this->maxSize = other.maxSize;
this->length = other.length;
this->p = new int[this->maxSize];
for (int index = 0; index < other.maxSize;
index++)
this->p[index] =
other.p[index];
}
void pointerDataClass::insertAt(int index, int num) \
{
this->p[index] = num;
this->length++;
}
void pointerDataClass::displayData()
{
for (int index = 0; index < maxSize; index++)
cout << this->p[index]
<< " ";
cout << endl;
}
void pointerDataClass::get()
{
cout << "Length: " << length <<
endl;
}
int main()
{
pointerDataClass list1(10);
list1.insertAt(0, 50);
list1.insertAt(4, 30);
list1.insertAt(8, 60);
cout << "List1: " << endl;
list1.displayData();
cout << "List 2: " << endl;
pointerDataClass list2(list1);
list2.displayData();
list1.insertAt(4, 100);
cout << "List1: (after insert 100 at indext 4) "
<< endl;
list1.displayData();
cout << "List 2: " << endl;
list2.displayData();
list1.get();
list2.get();
system("pause");
return 0;
}
SCREENSHOT -
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------