In: Computer Science
**** IN C++ ****
1) Modify the class pointerDataClass so the main function below is working properly. Use shallow 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;
}
Hi there,
I have solved the problem you were facing. There are 2 ways to solve the problem. Both depend on the output that you want. I have given both the solutions below. Choose the one which suits your needs.
If you want to use destructor in your class and want both the lists to have independent outputs, then you need code 1. Here I have changed the data structure of p to vector. When we have a pointer array all the objects point to the same array and the respective destructors try to delete the same array, therefore creating a error.
Code 1 :
#include<bits/stdc++.h>
using namespace std;
class pointerDataClass
{
int maxSize;
int length;
//here I used a vector instead of a pointer as pointer
objects
//dont really get copied in shallow copy.
vector <int> p;
public:
pointerDataClass(int size);
~pointerDataClass();
void insertAt(int index, int num);
void displayData();
};
//changed for vector
pointerDataClass::pointerDataClass(int size)
{
maxSize = size;
p.resize(maxSize);
length = 0;
}
pointerDataClass::~pointerDataClass()
{
p.clear();
p.shrink_to_fit();
length=0;
}
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<length;i++) {
cout<<p[i]<<" ";
}
}
int main()
{
pointerDataClass list1(10);
for (int i =0;i<6;i++)
{
list1.insertAt(i, i);
}
list1.insertAt(0, 50);
list1.insertAt(4, 30);
list1.insertAt(8, 60);
cout<<"\nList1: ";
list1.displayData();
cout<<"\nList 2: ";
pointerDataClass list2(list1);
list2.displayData();
list1.insertAt(4,100);
cout<<"\nList1: (after insert 100 at indext 4) ";
list1.displayData();
cout<<"\nList 2: ";
list2.displayData();
cout<<"\n";
return 0;
}
Code 1 (Screenshot) :
Output (Code 1) :
Now If you don't have any compulsion to use destructor but you want to use a pointer to store an array, then you should use code 2. Here I have deleted the destructor as both the object destructors were trying to delete the same pointer array, therefore producing the "double free" error.
Code 2 :
#include<bits/stdc++.h>
using namespace std;
class pointerDataClass
{
int maxSize;
int length;
int *p;
public:
pointerDataClass(int size);
void insertAt(int index, int num);
void displayData();
};
pointerDataClass::pointerDataClass(int size)
{
maxSize = size;
p=new int[maxSize];
length = 0;
}
//deleted the destructor
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<length;i++) {
cout<<p[i]<<" ";
}
}
int main()
{
pointerDataClass list1(10);
for (int i =0;i<6;i++)
{
list1.insertAt(i, i);
}
list1.insertAt(0, 50);
list1.insertAt(4, 30);
list1.insertAt(8, 60);
cout<<"\nList1: ";
list1.displayData();
cout<<"\nList 2: ";
pointerDataClass list2(list1);
list2.displayData();
list1.insertAt(4,100);
cout<<"\nList1: (after insert 100 at indext 4) ";
list1.displayData();
cout<<"\nList 2: ";
list2.displayData();
cout<<"\n";
return 0;
}
Code 2 (Screenshot) :
Output (Code 2) :
I hope you liked the answer. If you have any doubt, please do mention in the comment section.
Happy Coding :)