All code within the ****...**** need to be implemented. Variable Size Array with
ID: 3689057 • Letter: A
Question
All code within the ****...**** need to be implemented.
Variable Size Array with Classes, Testing.
Study Code and Object Definition Windows of Microsoft Visual Studio described here. As you work on the below project, demonstrate to the instructor the usage of this feature.
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 ***(http://vega.cs.kent.edu/~mikhail/classes/csi/Labs/Lab11/vararray.h)*****
. 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 ***(http://vega.cs.kent.edu/~mikhail/classes/csi/Labs/Lab11/test.cpp)****. 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.
Explanation / Answer
// variable size array class
#ifndef VARARRAY_H_
#define VARARRAY_H_
class varArray{
public:
varArray(){
dArray=new int[20];
} // 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_ */
//--------------------------------------------------------
// testing the implementation of class varArray
// Mikhail Nesterenko
// 4/01/2013
#include <iostream>
#include "vararray.h"
using std::cout; using std::endl;
void varArray::addNumber(int number)
{
dArray[size+1]=number;
size++;
}
void varArray::removeNumber(int number)
{
int pos=check(number);
if(pos!=-1)
{
for(int i=pos+1;i<size;i++)
{
dArray[i-1]=dArray[i];
}
size--;
}
else
{
cout<<endl<<"Given number is not present in array. So it can't be removed";
}
}
varArray::varArray(const varArray& B)
{
varArray A;
A=B;
}
varArray::~varArray()
{
delete[] dArray;
size=0;
}
varArray& varArray::operator=(const varArray& B)
{
varArray A;
A=B;
return A;
}
void varArray::output(){
for(int i=0;i<size;i++)
{
cout<<dArray[i]<<" ";
}
}
int varArray::check(int number)
{
int pos=-1;
for(int i=0;i<size;i++)
{
if(dArray[i]==number)
{
pos=i;
break;
}
}
return pos;
}
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
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.