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

Solutions

Expert Solution

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!!!!!!!!----------


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 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,...
**** 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[]...
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...
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 =...
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