Question
Objects with Dynamically Allocated Members Variable Size Array with Classes, Testing. Create a project titled Lab11_VarArrayTest. Implement the dynamically expanding and contracting integer array described
Objects with Dynamically Allocated Members
Variable Size Array with Classes, Testing.
Create a project titled Lab11_VarArrayTest. Implement the dynamically expanding and contracting integer array described in the previous lab as a class. You should use this class definition( check below). The class attributes are a pointer to the dynamically allocated array dAarray and the array size size This class contains two groups of methods:
Member functions output(), check() addNumber() and removeNumber() have exactly the same functionality as described in the previous lab.
copy constructor, overloaded assignment and destructor ensure correct handling of the objects with dynamically allocated members.
Your code should work with this test program(check below). It is designed to test your implementation of varArray class.
You are free to modify the class and the test program but you cannot change the principle of dynamic allocation of the array (i.e. you cannot make the member variables automatic) or remove testing of all the methods from the test program.
******this class definition**********
#ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray{ public: varArray(); // void constructor int arraySize() const {return size;} // returns the size of the array int check(int number); // returns index of element containg "number" or -1 if none void addNumber(int); // adds number to the array void removeNumber(int); // deletes the number from the array void output(); // prints the values of the array // big three varArray(const varArray&); // copy constructor varArray& operator=(const varArray&); // overloaded assignment ~varArray(); // destructor private: int *dArray; // pointer to the dynamically allocated array int size; // array size }; #endif /* VARARRAY_H_ */
********* this test program***********
#include#include "vararray.h" using std::cout; using std::endl; void testfunc(varArray); // function to test pass-by-value for varArray int main(){ varArray a1; // testing regular member functions a1.addNumber(1); a1.addNumber(2); a1.addNumber(3); a1.addNumber(3); // trying to add duplicate, should not add it cout << "a1 size is after number addition is: " << a1.arraySize() << endl; if(a1.check(1) != -1) // check() returns -1 if number not present cout << "1 is present in the array" << endl; if(a1.check(5) != -1) cout << "5 is present in the array" << endl; cout << "a1 before removal of 2: "; a1.output(); a1.removeNumber(2); cout << "a1 after removal of 2: "; a1.output(); // uncomment this when you debugged the first part /* testfunc(a1); // testing copy constructor cout << "a1 after testfunc call: "; a1.output(); // if destructor is implemented correctly // this should print properly after testfunc completes varArray a2,a3; a3=a2=a1; // testing stackability of the overloaded assignment cout << "a3 after stackable assingment: "; a3.output(); a3=a3; // testing protection against self-assingment cout << "a3 after self-assignment: "; a3.output(); */ } /* // tests pass-by-value for object of class varArray void testfunc(varArray va){ // copy constructor is invoked on "va" cout << "parameter va: "; va.output(); } // destructor is invoked when "va" goes out of scope */
Variable Size Array with Classes, Implementation. Create a project titled Lab11_VarArrayClasses. Using the class implemented in the first part of this lab, implement the functionality of the second part of the previous lab. That is, write a program that asks the user to input numbers to add to and remove from the array and then prints its contents.
Make sure your programs adhere to proper programming style. Submit your projects to the subversion repository. Do not forget to verify your submission on the web.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started