Question

In: Computer Science

**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...

**** 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;
}

Solutions

Expert Solution

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 :)


Related Solutions

**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** 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,...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha so the main function is working properly. #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class alpha { private: int data; public: //YOUR CODE }; //////////////////////////////////////////////////////////////// int main() { alpha a1(37); alpha a2; a2 = a1; cout << "\na2="; a2.display(); //display a2 alpha a3(a1); //invoke copy constructor cout << "\na3="; a3.display(); //display a3 alpha a4 = a1; cout << "\na4="; a4.display(); cout << endl; return 0;...
Modify the program below so the driver class (Employee10A) uses a polymorphic approach, meaning it should...
Modify the program below so the driver class (Employee10A) uses a polymorphic approach, meaning it should create an array of the superclass (Employee10A) to hold the subclass (HourlyEmployee10A, SalariedEmployee10A, & CommissionEmployee10A) objects, then load the array with the objects you create. Create one object of each subclass. The three subclasses inherited from the abstract superclass print the results using the overridden abstract method. Below is the source code for the driver class: public class EmployeeTest10A { public static void main(String[]...
Build a Date class and a main function to test it Specifications Below is the interface...
Build a Date class and a main function to test it Specifications Below is the interface for the Date class: it is our "contract" with you: you have to implement everything it describes, and show us that it works with a test harness that puts it through its paces. The comments in the interface below should be sufficient for you to understand the project (use these comments in your Date declaration), without the need of any further documentation. class Date...
Modify the FeetInches class so that it overloads the following operators: <= >= != Demonstrate the...
Modify the FeetInches class so that it overloads the following operators: <= >= != Demonstrate the class's capabilities in a simple program. this is what needs to be modified // Specification file for the FeetInches class #ifndef FEETINCHES_H #define FEETINCHES_H #include <iostream> using namespace std; class FeetInches; // Forward Declaration // Function Prototypes for Overloaded Stream Operators ostream &operator << (ostream &, const FeetInches &); istream &operator >> (istream &, FeetInches &); // The FeetInches class holds distances or measurements...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates the Fibonacci sequence and writes the sequence to the shared-memory object. The Consumer.c file should then output the sequence. Producer.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/mman.h> #include <zconf.h> int main() { /* The size (in bytes) of shared-memory object */ const int SIZE = 4096; /* The name of shared-memory object */ const char *Obj =...
Using C++ 1. Create a main function in a main.cpp file. The main function should look...
Using C++ 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} 2. Create an array. 3. Ask user to enter numbers in size of your array. 4. Take the numbers and store them in your array. 5. Go through your array and add all the numbers. 6. Calculate the average of the numbers. 7. Display the numbers, sum and average.
C++ 1. Modify this program to open the file "Customers.dat" so that all data is written...
C++ 1. Modify this program to open the file "Customers.dat" so that all data is written to the end of the file AND to allow the file to be read. 2. Create a method called "getLargestCustomerNumber" and call it after the "Customers.dat" file has been opened. Read all the existing customerNumbers and determine the largest customerNumber - do not assume the last record in the file is the largest number. Use this number as the base when adding new customer...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...
1. Suggest a way to modify the laboratory procedure (offered below) so that the reaction produces...
1. Suggest a way to modify the laboratory procedure (offered below) so that the reaction produces monobenzalacetone instead of dibenzalacetone. Up next i offer the procedure that we went through in the lab: Lab Title: Dibenzalacetone synthesis Objective: Conduct a mixed aldol condensation reaction using acetone and benzaldehyde. Procedure: 1. In a test tube place 50 mL of NaOH at 10% w/v, 40 mL of 95% ethanol, 5.4 g of benzaldehyde and 1.5 g of acetone. 2. Close the tube...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT