Question: Use source code: Header file: Main file: In the main program, add lines of code to perform the following on a dynamically allocated single instance
Use source code:

Header file:

Main file:

- In the main program, add lines of code to perform the following on a dynamically allocated single instance of the class:
a) Use new to create a single instance of the class
b) Write instructions that use pointer dereferencing and dot operator to set that class instance's name and number. For example, (*ptr).setName()...
c) Write additional instructions that use the arrow operator to do the same thing as in (b). For example: ptr -> setName()...
d) print the values at each step to verify it worked.
e) use delete to return the dynamically allocated instance to the freestore. Be sure to set the pointer to NULL after that.
2. In the main program, add lines of code to perform the following on a dynamically allocated array:
a) Use new to allocate an array of 20 instances of the SmallClass class.
b) create an auxiliary (extra) pointer, set to also point at this array, so that it can be iterated (you do not want to use the original pointer, as its original base address of the array would be lost).
c) Use a for or while loop to iterate through the elements. The auxiliary pointer should be incremented at each step.
d) In the loop, use the arrow notation with the auxiliary pointer to set the number of each array element to i*2, where i is the
index of the element.
e) subsequently, print the element out so that it verifies each element was set correctly.
3. In the main program, add these lines of code based on the array:
a) Create another array pointer, such that you allocate an array having 10 more elements than the first one.
b) Copy the 20 elements from the first array to the second (the second array's last 10 would thus remain unchanged). Using standard array access with square brackets is OK.
c) Iterate through this array to print all elements out to verify that the copy of the first 20 elements worked.
After (2) and (3) are done, add lines to return the arrays to the freestore. Be sure to set recently deleted pointers to NULL!
#include #include #include using namespace std; #include "smallclass.h" #include "smallclass.cpp" int main() { int i; SmallClass *sptr1; // Iterator variable. // Pointers used in a few places. SmallClass *sptr2; Smallclass *sptr3; //********************************************************** // Part 4: dynamically create a new single instance and use // pointers to make changes to it, printing them out. // use the dereferencing, and the arrow operator //********* //********** // Part 5: dynamically allocate an array of 20 instances. // Use an extra pointer, with incrementing of the pointer // inside a for loop. Use the arrow operator to call the // setNumber such that element i's number is i * 2. //** // Part 6: dynamically allocate another array of 30 // instances. Then use a for loop to copy the first 20 from // the first array to the second. Print all of the values // of the second array to verify the copy worked. //** //** // Not an official numbered "part", but delete the arrays // and set their pointers to NULL. //*******************************: ******** return 0; }
Step by Step Solution
There are 3 Steps involved in it
To implement the tasks described you will need to modify the main function Below is a stepbystep gui... View full answer
Get step-by-step solutions from verified subject matter experts
