Give the definitions for the member function addValue, the copy constructor, the overloaded assignment operator, the overloaded operator<<, and the destructor for the following class. This class is intended to be a class for a partially filled array. The member variable numberUsed contains the number of array positions currently filled. The other constructor definition is given to help you get started.
private: double *a; int maxNumber; int numberUsed; };
PartFilledArray::PartFilledArray(int arraySize) : maxNumber(arraySize), numberUsed(0) { a = newdouble[maxNumber]; }
// Member function addValue voidPartFilledArray::addValue(double newEntry){ if (numberUsed < maxNumber) { a[numberUsed] = newEntry; numberUsed++; } else { cout << "Error: Adding value to a full array.\n"; exit(EXIT_FAILURE); } }
// Copy constructor PartFilledArray::PartFilledArray(const PartFilledArray& object) : maxNumber(object.maxNumber), numberUsed(object.numberUsed) { a = newdouble[maxNumber]; for (int i = 0; i < numberUsed; i++) { a[i] = object.a[i]; } }
// Overloaded assignment operator void PartFilledArray::operator =(const PartFilledArray& rightSide) { if (this != &rightSide) { delete [] a; maxNumber = rightSide.maxNumber; numberUsed = rightSide.numberUsed; a = newdouble[maxNumber]; for (int i = 0; i < numberUsed; i++) { a[i] = rightSide.a[i]; } } }
// Overloaded operator<< ostream& operator<<(ostream& outs, const PartFilledArray& object) { for (int i = 0; i < object.numberUsed; i++) { outs << object.a[i] << " "; } return outs; }
intmain(){ // Create a PartFilledArray object of size 5 PartFilledArray pfa1(5);
// Add values to pfa1 pfa1.addValue(1.1); pfa1.addValue(2.2); pfa1.addValue(3.3);
cout << "pfa1: " << pfa1 << endl;
// Create a new PartFilledArray object using the copy constructor PartFilledArray pfa2(pfa1);
cout << "pfa2: " << pfa2 << endl;
// Create a PartFilledArray object of size 10 PartFilledArray pfa3(10);
// Add values to pfa3 for (int i = 0; i < 10; i++) { pfa3.addValue(i * 1.1); }
cout << "pfa3: " << pfa3 << endl;
// Use the assignment operator to assign the value of pfa3 to pfa1 pfa1 = pfa3;
cout << "pfa1: " << pfa1 << endl;
return0; }
2. (Continue to the previous question)
The Question:
Define a class called PartFilledArrayWMax that is a derived class of the class PartFilledArray. The class PartFilledArrayWMax has one additional member variable named maxValue that holds the maximum value stored in the array. Define a member accessor function named getMax that returns the maximum value stored in the array. Redefine the member function addValue and define two constructors, one of which has an int argument for the maximum number of entries in the array, another is a copy constructor. Also define an overloaded assignment operator, and a destructor.