Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part A: Working with float vector using Array A class FloatVectArray has been de

ID: 3885727 • Letter: P

Question

Part A: Working with float vector using Array

A class FloatVectArray has been declared and needs to be implemented using an array of type float to set its elements. First, complete the class by finishing the code for the default and copy constructors, the assignment operator and the destructor. Then use the main function to test it.

#include <iostream>

using namespace std;

class FloatVectArray {

public:

FloatVectArray(int vsize = 10);    // default constructor

FloatVectArray(float vlist[], int arraysize); // constructor with init parameters

FloatVectArray(const FloatVectArray& fva); // copy constructor

FloatVectArray& operator = (const FloatVectArray& fva); // assignment operator

~FloatVectArray();    // destructor

void printElements(); // prints all elements in the vector

private:

    float *vector;        // the vector

    int vectorsize;    // size of the vector

};

// default constructor

FloatVectArray::FloatVectArray(int vsize) {

// To be completed   

}

// constructor with initialization parameters

FloatVectArray::FloatVectArray(float vlist[], int arraysize) {

// To be completed   

}

// copy constructor

FloatVectArray::FloatVectArray(const FloatVectArray& fva) {

To be completed   

}

// assignment operator

FloatVectArray& FloatVectArray::operator = (const FloatVectArray &fva) {

// To be completed   

}

// destructor

FloatVectArray::~FloatVectArray() {

// To be completed   

}

void FloatVectArray::printElements() {

// To be completed   

}

int main() {

// testing the implementation of a float vector using array

// declare & initialize an array of floats and create a float vector aVect

float FloatArray1[10] = {2.1, 7.2, 3.3, 6.4, 1.5, 43.6,72.7,19.8,39.9, 71.1};

FloatVectArray aVect(FloatArray1, 10);

cout << "Printing all elements in aVect:" << endl;

aVect.printElements();

// declare & initialize another array of floats and create float vector bVect

float FloatArray2[10] = {1.1, 2.2, 5.3, 6.4, 1.5, 7.6, 9.7,17.8, 92.9, 42.1};

FloatVectArray bVect(FloatArray2, 10);

cout << "Printing all elements in bVect:" << endl;

bVect.printElements();

// try the assignment constructor

bVect = aVect;

cout << "Assigned bVect = aVect. Printing now all elements in bVect:" <<endl;

bVect.printElements();

// try the copy constructor

FloatVectArray cVect = aVect;

cout << "cVect is a copy of aVect. Printing all elements in cVect:" << endl;

cVect.printElements();

return 0;

}

Turn in the complete code, including all of the above “to be completed” sections.

This code must be in c++

Explanation / Answer

PROGRAM CODE:

#include <iostream>

using namespace std;

class FloatVectArray {

public:

FloatVectArray(int vsize = 10); // default constructor

FloatVectArray(float vlist[], int arraysize); // constructor with init parameters

FloatVectArray(const FloatVectArray& fva); // copy constructor

FloatVectArray& operator = (const FloatVectArray& fva); // assignment operator

~FloatVectArray(); // destructor

void printElements(); // prints all elements in the vector

private:

float *vector; // the vector

int vectorsize; // size of the vector

};

// default constructor

FloatVectArray::FloatVectArray(int vsize) {

vector = new float[vsize];

vectorsize = vsize;

}

// constructor with initialization parameters

FloatVectArray::FloatVectArray(float vlist[], int arraysize) {

vector = new float[arraysize];

vectorsize = arraysize;

for(int i=0; i<arraysize; i++)

vector[i] = vlist[i];

}

// copy constructor

FloatVectArray::FloatVectArray(const FloatVectArray& fva) {

vector = new float[fva.vectorsize];

vectorsize = fva.vectorsize;

for(int i=0; i<vectorsize; i++)

vector[i] = fva.vector[i];

}

// assignment operator

FloatVectArray& FloatVectArray::operator = (const FloatVectArray &fva) {

this->vector = fva.vector;

this->vectorsize = fva.vectorsize;

return *this;

}

// destructor

FloatVectArray::~FloatVectArray() {

vectorsize = 0;

delete [] vector;

}

void FloatVectArray::printElements() {

cout<<endl;

for(int i=0; i<vectorsize; i++)

{

cout<<vector[i]<<" ";

}

cout<<endl;

}

int main() {

// testing the implementation of a float vector using array

// declare & initialize an array of floats and create a float vector aVect

float FloatArray1[10] = {2.1, 7.2, 3.3, 6.4, 1.5, 43.6,72.7,19.8,39.9, 71.1};

FloatVectArray aVect(FloatArray1, 10);

cout << "Printing all elements in aVect:" << endl;

aVect.printElements();

// declare & initialize another array of floats and create float vector bVect

float FloatArray2[10] = {1.1, 2.2, 5.3, 6.4, 1.5, 7.6, 9.7,17.8, 92.9, 42.1};

FloatVectArray bVect(FloatArray2, 10);

cout << "Printing all elements in bVect:" << endl;

bVect.printElements();

// try the assignment constructor

bVect = aVect;

cout << "Assigned bVect = aVect. Printing now all elements in bVect:" <<endl;

bVect.printElements();

// try the copy constructor

FloatVectArray cVect = aVect;

cout << "cVect is a copy of aVect. Printing all elements in cVect:" << endl;

cVect.printElements();

return 0;

}

OUTPUT: