Question
Create a project titled Lab11_VarArrayTest. Implement the dynamically expanding and contracting array of doubles described in the previous lab as a class. You should use
Create a project titled Lab11_VarArrayTest. Implement the dynamically expanding and contracting array of doubles described in the previous lab as a class. You should use this class definition. 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.
The header file, test file and function definition file are all posted below. I just need the return statement in the funcDef.cpp file in line 57. Everything else should be working fine.
The vararray.h file:
#ifndef VARARRAY_H_
#define VARARRAY_H_
#include
using namespace std;
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
The test.cpp file:
#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 << "array size 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;
a1.output();
a1.removeNumber(2);
a1.output();
// uncomment this when you debugged the first part
varArray a2, a3;
a3 = a2 = a1; // testing overloaded assignment
a3 = a3; // testing protection against self-assingment
testfunc(a3); // testing copy constructor
a3.output(); // if destructor is implemented correctly
// this should print properly after testfunc completes
}
// tests pass-by-value for object of class varArray
void testfunc(varArray va) { // copy constructor is invoked on "va"
va.output();
} // destructor is invoked when "va" goes out of scope
The funcDef.cpp file:
#include "varArray.h"
// function checks the array for the number
int varArray::check(int number) {
for (int i = 0; i < size; i++) {
if (dArray[i] == number) {
return i;
}
}
return -1;
}
// adds number to array
void varArray::addNumber(int number) {
int *tmp;
tmp = new int[size + 1];
for (int i = 0; i < size; i++) {
tmp[i] = dArray[i];
}
tmp[size] = number;
size++;
delete dArray;
dArray = tmp;
}
// removes number from array
void varArray::removeNumber(int number) {
int index = check(number);
if (index < 0) {
return;
}
int *tmp = new int[size - 1];
for (int i = 0; i < index; i++) {
tmp[i] = dArray[i];
}
for (int i = index + 1; i < size; i++) {
tmp[i - 1] = dArray[i];
}
size--;
delete dArray;
dArray = tmp;
}
// Outputs Array
void varArray::output() {
for (int i = 0; i < size; i++) {
cout << dArray[i] << ' ';
}
}
varArray::varArray(const varArray &)
{
}
varArray & varArray::operator=(const varArray &)
{
// TODO: insert return statement here
}
varArray::~varArray()
{
}
// Void Constructor
varArray::varArray() {
size = 0;
}
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