Reasking question because last answer was very vague screenshots of code!!!! Ple
ID: 3860809 • Letter: R
Question
Reasking question because last answer was very vague screenshots of code!!!! Please dont waste my money
Redesign your Array class from lab6 as a class template to work with the application below. Due to theg++ compiler, your class declaration and class method definitions must both reside in the header file, Array.h. Do not create a separate Array.C file for your class template. In addition, write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below.
#include "Array.h"
main()
{
Array<char> c(3);
c.setValue(0,'c');
c.setValue(1,'s');
c.setValue(2,'c');
cout << c;
Array<int> i(3);
i.setValue(0,1);
i.setValue(1,2);
i.setValue(2,5);
cout << i;
Array<int> j(3);
j.setValue(0,10);
j.setValue(1,20);
j.setValue(2,50);
cout << j;
Array<int> ij;
ij = i + j;
cout << ij;
}
The output for this program should be as follows:
Array:
size: 3
contents: c s c
Array:
size: 3
contents: 1 2 5
Array:
size: 3
contents: 10 20 50
Array:
size: 3
contents: 11 22 55
My Lab6
Array.h
#ifndef ARRAY_H_
#define ARRAY_H_
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
#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
Explanation / Answer
//Array.h
#ifndef ARRAY_H_
#define ARRAY_H_
#include <iostream>
using namespace std;
template<class T>
class Array
{
public:
Array(void)
{
this->n = 0;
this->f = NULL;
return;
}
Array(int n)
{
this->n = n;
this->f = new T[n];
return;
}
Array(const Array &a)
{
//if (f) delete[] this->f;
*this = a;
return;
}
~Array()
{
if (f) delete[] this->f;
return;
}
int getSize(void)
{
return this->n;
}
int setValue(int n, T v)
{
if ((n >= 0) && (n < this->n))
{
this->f[n] = v;
return 0; // index in range
}
return 1; // index out of range
}
T *getAddress(void) // Debugging
{
return this->f; // This is for printing object to cout, and also debugging
}
T getValue(int n)
{
return this->f[n];
}
void 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 T[this->n];
for (d = 0; d < this->n; d++) this->f[d] = a.f[d];
}
return;
}
friend 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;
}
friend Array operator +(Array &a, T b)
{
int d;
Array n(a.getSize());
for (d = 0; d < a.getSize(); d++) n.setValue(d, a.getValue(d) + b);
return n;
}
inline friend 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;
}
private:
T *f;
int n;
};
#endif
---------------------------------------------------------
//main.cpp, u can change this to test different data types, But I tested three data types in one main ,
#include"Array.h"
int main()
{
//Array of float
Array<float> a1(3);
a1.setValue(0, 1.0);
a1.setValue(1, 22.0);
a1.setValue(2, 12.2);
cout << "a1 = "<<a1 << endl;
Array<float> a2(3);
a2.setValue(0, 3.3);
a2.setValue(1, 44.5);
a2.setValue(2, 21.7);
cout << "a2 "<<a2 << endl;
Array<float> tmp;
tmp = a1 + a2;
cout << tmp;
Array<float> tmp2;
tmp2 = tmp + 10.0;
cout << tmp2;
//Added by chegA, check for array of int
Array<char> c(3);
c.setValue(0, 'c');
c.setValue(1, 's');
c.setValue(2, 'c');
cout << c;
Array<int> i(3);
i.setValue(0, 1);
i.setValue(1, 2);
i.setValue(2, 5);
cout << i;
Array<int> j(3);
j.setValue(0, 10);
j.setValue(1, 20);
j.setValue(2, 50);
cout << j;
Array<int> ij;
ij = i + j;
cout << ij;
return 0;
}
---------------------------------------------------------------------------------
//output
a1 = Size: 3
Array addr: 0016F7CC
Array Contents:
1
22
12.2
a2 Size: 3
Array addr: 0016F7BC
Array Contents:
3.3
44.5
21.7
Size: 3
Array addr: 0016F7AC
Array Contents:
4.3
66.5
33.9
Size: 3
Array addr: 0016F79C
Array Contents:
14.3
76.5
43.9
Size: 3
Array addr: 0016F78C
Array Contents:
c
s
c
Size: 3
Array addr: 0016F77C
Array Contents:
1
2
5
Size: 3
Array addr: 0016F76C
Array Contents:
10
20
50
Size: 3
Array addr: 0016F75C
Array Contents:
11
22
55
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.