Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++: Help on Objects w/Dynamically Allocated Members and Templates varList.h //This file expected to be used in parts 1 and 2. //Plase try not

In C++:

Help on Objects w/Dynamically Allocated Members and Templates

image text in transcribed

image text in transcribed

varList.h

//This file expected to be used in parts 1 and 2. //Plase try not to change this interface. //please notice the template file inclusion. "implementation.template" #ifndef VARLIST_H #define VARLIST_H template  class VarList { public: VarList(); // void constructor int getSize() const { return size; } // returns the current size of the array int check(T key); // returns index of element holding "key" or -1 if none void addValue(T); // adds number to the array void removeValue(T); // deletes the number from the array void printList(); // prints the values of the array //// big three rule VarList(const VarList&); // copy constructor VarList& operator=(const VarList&); // overloaded assignment ~VarList(); // destructor private: T* dArray; // pointer to the dynamically allocated array int size; // array size }; #include "implementation.template" //very important to include this template file where your methods need to be implemented. #endif /* VARLIST_H */

testMain.cpp

#include  #include  #include  #include "varList.h" using namespace std; template  void testingCpyConstruct(VarList); int main() { srand(time(nullptr) + rand() % 100); //better seed VarList charList; if (charList.getSize() == 0) cout  

for (int i = 0; i -1);charList.addValue( tmp); if (!(tmp >= 'A' && tmp

 cout > properData; if (properData != 'Y' && properData != 'y') { cout -1) { cout > properData; if (properData == 'Y' && properData == 'y') { cout  charList2, charList3; //local variables charList3 = charList2 = charList; // testing overloaded assignment charList3 = charList3; // testing protection against self-assingment cout  void testingCpyConstruct(VarList receivedList) { // copy constructor is invoked on "receivedList" receivedList.printList(); } //// destructor is invoked when "receivedList" goes out of its scope since it is a method parameter
Project description: Write a program that allows the user to add or remove value to a collection of a given data type (primitive or programmer-defined data types such as a class or struct) and then print all of them. A user may enter the value twice in which case, the repeated value should not be entered. Here is an example program dialog, assuming the list created to hold integer values: Enter operation [A/R/Q] : A Enter a value :10 Current values stored in the list: 10 Enter operation [A/R/Q] : A Enter a value :20 Current values stored in the list: 10,20 Enter operation [A/R/Q] : A Enter a value :20 Current values stored in the list: 10,20 Enter operation [A/R/Q]: R Enter a value :10 Current values stored in the list: 20 Enter operation [A/R/Q]: A Enter a value :7 Current values stored in the list: 20,7 Enter operation [A/R/Q]: A Enter a value :15 Current values stored in the list: 20,7,15 Enter operation [A/R/Q]: R Enter a value :7 Current values stored in the list: 20,15 Enter operation [A/R/Q]: Q The size of the user input can be arbitrarily large. For this program, to accommodate user input, you need to implement. an underlining array whose size varies as necessary. The values in the array should not be sorted. Note: In this project, you will create two C++ projects, one to implement the missing methods (inside a template file "implementation.template") and the second to use what has been implemented and tested, in part 1 , to write the target program. That is, the same way you did in the battleship game project. Part 1: Variable Size Array with Classes, Testing. Create a project titled Testing_VarArray_Components. Implement the dynamically expanding and contracting generic array described above in an OOP style. You should use the header file posted on Canvas (code is given below for your convenience). The class attributes are a pointer to the dynamically allocated array dAarray and the array size listSize. This class contains two groups of methods: - Member functionsprintList(), check() addValue() and removeValue(0 as described below. - copy constructor, overloaded assignment, and destructor ensure correct handling of the objects with dynamically allocated members. - Note: All the class method implementations must be implemented in the "template" file that must be included in the header file and protected against multi inclusion. Your code should work with the test posted on Canvas (and provided below). It is designed to test your implementation of varArray class. - printList()prints the arrays' contents since it is a member of the same class. - check())akes a value (key) and checks if the value is in the array. If the value is in the array, returns the index of the element of the array holding the value. Otherwise, returns 1. - addValue()takes and adds a value (of a proper data type) to the array if the value is not already there. Note that the array cannot be enlarged to accommodate the new value. Instead, this function needs to allocate a new temporary array whose size is one more than the size of the old array, copy the contents of the old array to the new one, including the new value, deallocate the old array and then assign the address of the new array back to the original pointer. Notice that since it is a member of the same class., the call to this function results in effectively "enlarging" the array pointed to by the pointer. (remember the Basicvector example (-)) - removeValue()takes a value (key) and removes the value if it is present in the array. If the value (key) is removed, the array shrinks. Note that the function should not change an array in any way if the value (key) is not present in it. If the value is present, then the function should allocate a new array of smaller size and then copy all the values to it, except the one that needs to be erased. After copying is done, deallocate the old array and update the array pointer to point to the new array. Copying and removing one element may be done in a single for-loop. Part 2: Variable Size Array with Classes, Implementation. Create a project titled VarArray_Project. Using the class implemented in the first part of this lab, write a program that asks the user to input values to add to and remove from the array and then print its contents. In this project, reuse the header file given to you and the template file that contains the implementation of all of the class methods. Make sure your programs adhere to the proper programming style. Submit your projects to the designated dropbox on Canvas. Please make sure to get your work checked off by the TS (SI). Treat this assignment as a learning project not only an evaluation component

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Support For Data Mining Applications Discovering Knowledge With Inductive Queries Lnai 2682

Authors: Rosa Meo ,Pier L. Lanzi ,Mika Klemettinen

2004th Edition

3540224793, 978-3540224792

More Books

Students also viewed these Databases questions