Question: Redesign your Array class from lab6 as a class template to work with the application below. Due to the g++ compiler, your class declaration and

Redesign your Array class from lab6 as a class template to work with the application below. Due to the g++ 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 c(3);

c.setValue(0,'c');

c.setValue(1,'s');

c.setValue(2,'c');

cout << c;

Array i(3);

i.setValue(0,1);

i.setValue(1,2);

i.setValue(2,5);

cout << i;

Array j(3);

j.setValue(0,10);

j.setValue(1,20);

j.setValue(2,50);

cout << j;

Array 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

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

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

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

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!