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...
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 =...
**Keil uVision5 - ARM Cortex M0+ - Embedded C programming Modify the program below so that...
**Keil uVision5 - ARM Cortex M0+ - Embedded C programming Modify the program below so that the Red, Green, and Blue LEDs are switched ON and OFF in a sequence with one second delay for Red and Green LEDs, and 0.5 second delay for the Blue LED. #include "MKL25Z4.h" void delay(int n); int main (void) { SIM_SCGC5 |= SIM_SCGC5_PORTB(1); /* enable clock to Port B */ PORTB_PCR18 |=PORT_PCR_MUX(1); /* Configure PORTB ,pin 18 as GPIO ; set MUX*/ GPIOB_PDDR=(1UL <<...
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++ Modify FunctionTable.cpp so each that each function returns a string(instead of printing out a message)...
C++ Modify FunctionTable.cpp so each that each function returns a string(instead of printing out a message) and so that this value is printed inside of main(). //: C03:FunctionTable.cpp // Using an array of pointers to functions #include <iostream> using namespace std; // A macro to define dummy functions: #define DF(N) void N() { \ cout << "function " #N " called..." << endl; } DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g); void (*func_table[])() = { a, b, c, d, e,...
Exercise 1 (a) Use javascript modify the code below, so that in addition to outputting the...
Exercise 1 (a) Use javascript modify the code below, so that in addition to outputting the selection to the web page, the selection is also placed in the browser’s local storage. Use ‘select’ as the local-storage key. The value will be the name of the category that was selected or the empty string is no selection was made. (d) Add a button called ‘retrieve’. When it is clicked the local storage is read and the prior selection is shown on...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT