Lab 6 – Operator, Copy, and Stream overloading Design an Array class to operate
ID: 3858174 • Letter: L
Question
Lab 6 – Operator, Copy, and Stream overloading
Design an Array class to operate on and store any number of floating-point values. Write a constructor to accept the amount of floating-point values to dynamically create and a destructor that performs the necessary cleanup and de-allocation. Write an overloaded operator function to perform addition on 2 objects with the same number of floating-point values. Overload the operator to simply add the corresponding floating-point values between two Array objects. In addition, write an overloaded operator function to perform addition between an Array object and a single floating-point value (add the float to each of the array values). Override the default copy constructor and assignment operator to perform a deep-copy. Lastly, overload the output stream operator to print the state of an Array object. Write a small application, similar to the one shown below, to test your class. As before write your Array class in separate source and header files (.C and .h) and the application in a separate file (lab6.C).
using namespace std;
#include <iostream>
#include “Array.h”
main()
{
Array a1( 3 );
a1.setValue( 0, 1.0 );
a1.setValue( 1, 22.0 );
a1.setValue( 2, 12.2 );
Array a2( 3 );
a2.setValue( 0, 3.3 );
a2.setValue( 1, 44.5 );
a2.setValue( 2, 21.7 );
Array tmp;
tmp = a1 + a2;
cout << tmp;
}
Explanation / Answer
Below is your code. Note that Array is a class and C program does not contain class. C++ contains classes so name will not be like .c, it will be like .cpp.
Array.h
#ifndef ARRAY_H_
#define ARRAY_H_
#include <iostream>
using namespace std;
class Array
{
public:
Array(void);
Array(int n);
Array(const Array &a);
~Array();
int getSize(void);
int setValue(int n, float v);
float *getAddress(void); // Debugging
float getValue(int n);
void operator =(const Array &a);
friend Array operator +(Array &a, Array &b);
friend Array operator +(Array &a, float b);
friend ostream &operator <<(ostream &output, const Array &a);
private:
float *f;
int n;
};
#endif
Array.cpp
#include "Array.h"
Array::Array(void)
{
this->n = 0;
this->f = 0;
return;
}
Array::Array(int n)
{
this->n = n;
this->f = new float[n];
return;
}
Array::Array(const Array &a)
{
if(f) delete[] this->f;
*this = a;
return;
}
Array::~Array()
{
if(f) delete[] this->f;
return;
}
int Array::getSize(void)
{
return this->n;
}
int Array::setValue(int n, float v)
{
if((n >= 0) && (n < this->n))
{
this->f[n] = v;
return 0; // index in range
}
return 1; // index out of range
}
float *Array::getAddress(void)
{
return this->f; // This is for printing object to cout, and also debugging
}
float Array::getValue(int n)
{
return this->f[n];
}
void Array::operator =(const Array &a)
{
int d;
if(this != &a) // Don't copy itself
{
if(f) delete[] this->f;
this->n = a.n;
this->f = new float[this->n];
for(d = 0; d < this->n; d++) this->f[d] = a.f[d];
}
return;
}
Array operator +(Array &a, Array &b)
{
int c, d;
// If arrays a and b are different sizes, only use the number of floats
// equal to the smallest array (to prevent accessing outside an array)
if(a.getSize() < b.getSize()) c = a.getSize();
else c = b.getSize();
Array n(c);
for(d = 0; d < c; d++) n.setValue(d, a.getValue(d) + b.getValue(d));
return n;
}
Array operator +(Array &a, float b)
{
int d;
Array n(a.getSize());
for(d = 0; d < a.getSize(); d++) n.setValue(d, a.getValue(d) + b);
return n;
}
ostream &operator <<(ostream &output, const Array &a)
{
output << "Size: " << a.n << endl;
output << "Array addr: " << a.f << endl;
output << "Array Contents:" << endl;
for(int b = 0; b < a.n; b++) output << a.f[b] << endl;
return output;
}
Lab6.cpp
#include "Array.h"
#include <iostream>
using namespace std;
int main(void)
{
Array a1( 3 );
a1.setValue( 0, 1.0 );
a1.setValue( 1, 22.0 );
a1.setValue( 2, 12.2 );
Array a2( 3 );
a2.setValue( 0, 3.3 );
a2.setValue( 1, 44.5 );
a2.setValue( 2, 21.7 );
Array tmp;
tmp = a1 + a2;
cout << tmp;
Array tmp2;
tmp2 = tmp + 10.0;
cout << tmp2;
return 0;
}
Output: -
Size: 3
Array addr: 0x5479d0
Array Contents:
4.3
66.5
33.9
Size: 3
Array addr: 0x547b60
Array Contents:
14.3
76.5
43.9
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.