Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I need to see if my program follows the specifications in the test driver or if needs some modifications about the requeriments described below.

Hi, I need to see if my program follows the specifications in the test driver or if needs some modifications about the requeriments described below. Thanks

Write a data structures template. The resulting template can be used in any program in place of a C++ array, without having to copy/paste the class code the way you did with class Array in Assignment 1.

Requirements. Develop MyStaticArray.h as you write a test driver CPP with class MyStaticArray, defined and fully tested. Write the public interface exactly as specified below -- do not add to, or change the public interface as specified.

Write the template for an array of values of unspecified type.

Let the capacity be set as part of the template specification, as modeled in the reading.

Include a square bracket getter and a setter, both with index range-checking, returning whatever value you wish, if out of range.

Include a getter named MyStaticArray::capacity( ) to return the data structure's capacity.

Initialize all data members to their default values in the constructor. That includes setting the array elements to their default data type.

Do tests with int, double, or char. Also do tests with an object, like string.

Do NOT write any other functions in the public interface.

CODE:

Header:

#ifndef StaticArray_h

#define StaticArray_h

#define CAP 100

template

class StaticArray

{

V values[CAP];

V dummy;

public:

StaticArray();

int capacity() const { return CAP; }

V operator[](int) const;

V& operator[](int);

};

template

StaticArray ::StaticArray()

{

for (int index = 0; index < CAP; index++)

values[index] = V();

}

template

V StaticArray ::operator[](int index) const

{

if (index < 0 || index >= CAP)

return V(); // a copy

return values[index]; // a copy

}

template

V& StaticArray ::operator[](int index)

{

if (index < 0 || index >= CAP)

return dummy; // a mutable reference

return values[index]; // a mutable reference

}

#endif

Test Driver:

#include

#include

using namespace std;

#include

#include "StaticArray.h"

#include "StaticArray.h"

int main()

{

//inyeger type test

StaticArray a;

cout << " *******Using int values:******" << endl;

cout << " Testing Array::Array ";

for (int i = 0; i < a.capacity(); i++)

assert(a[i] == 0);

cout << " Testing the Array::operator[ ] setter ";

a[1] = 123;

a[2] = 546;

cout << "EXPECTED: 123 for a[1] ";

cout << "ACTUAL: " << a[1] << endl;

assert(123 == a[1]);

cout << "EXPECTED: 546 for a[2] ";

cout << "ACTUAL: " << a[2] << endl;

assert(546 == a[2]);

a[-1000] = 222;

cout << "EXPECTED: 222 for a[-1000] ";

cout << "ACTUAL: " << a[-1000] << endl;

//assert different values

assert(123 == a[1]);

assert(546 == a[2]);

assert(222 == a[-3]);

assert(222 == a[100]);

assert(222 != a[11]);

assert(222 != a[0]);

cout << " Testing the Array::operator[ ] getter ";

const StaticArray b = a;

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

assert(a[i] == b[i]);

//contant object test

cout << " ********Const object test********* " << endl;

const StaticArray d1;

assert(d1.capacity());

assert(d1[0] == d1[0]);

cout << "Using string values const test:" << endl;

cout << " Testing Array::Array ";

for (int i = 0; i < a.capacity(); i++)

assert(d1[i] == "");

cout << " Testing the Array::operator[ ] setter ";

//assign new values

d1[1] = "New";

d1[2] = "string";

cout << " EXPECTED: for c[1] " << d1[1];

cout << "ACTUAL: " << d1[1] << endl;

assert("" == d1[1]);

cout << " ***********Using string class object test********* " << endl;

StaticArray c;

assert(c.capacity());

assert(c[0] == c[0]);

cout << "Using string values: " << endl;

cout << "Testing Array::Array ";

for (int i = 0; i < a.capacity(); i++)

assert(c[i] == "");

cout << " Testing the Array::operator[ ] setter ";

c[1] = "New";

c[2] = "string";

cout << " EXPECTED: \"New\" for c[1] ";

cout << "ACTUAL: " << c[1] << endl;

assert("New" == c[1]);

cout << "EXPECTED: \"string\"for a[2] ";

cout << "ACTUAL: " << c[2] << endl;

assert("string" == c[2]);

c[-1000] = "test";

cout << "EXPECTED: \"test\" for c[-1000] ";

cout << "ACTUAL: " << c[-1000] << endl;

//assert different values

assert("New" == c[1]);

assert("string" == c[2]);

assert("test" == c[-3]);

assert("test" == c[100]);

assert("test" != c[11]);

assert("test" != c[0]);

cout << " Testing the Array::operator[ ] getter ";

const StaticArray d = c;

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

assert(c[i] == d[i]);

//double test

StaticArray e;

cout << " **********Using double values************ " << endl;

cout << "Testing Array::Array ";

for (int i = 0; i < e.capacity(); i++)

assert(e[i] == 0.0);

cout << " Testing the Array::operator[ ] setter ";

e[1] = 12.3;

e[2] = 5.46;

cout << " EXPECTED: 12.3 for e[1] ";

cout << "ACTUAL: " << e[1] << endl;

assert(12.3 == e[1]);

cout << "EXPECTED: 5.46 for e[2] ";

cout << "ACTUAL: " << e[2] << endl;

assert(5.46 == e[2]);

e[-1000] = 2.22;

cout << "EXPECTED: 2.22 for e[-1000] ";

cout << "ACTUAL: " << e[-1000] << endl;

//assert different values

assert(12.3 == e[1]);

assert(5.46 == e[2]);

assert(2.22 == e[-3]);

assert(2.22 == e[100]);

assert(2.22 != e[11]);

assert(2.22 != e[0]);

cout << " Testing the Array::operator[ ] getter ";

const StaticArray f = e;

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

assert(e[i] == f[i]);

//-----------------------------------------------------

//boolean test already given

StaticArray a1;

cout << " *************Using bool values:********** " << endl;

cout << "Testing Array::Array ";

for (int i = 0; i < a1.capacity(); i++)

assert(a1[i] == 0);

cout << " Testing the Array::operator[ ] setter ";

a1[1] = true;

a1[2] = false;

cout << " EXPECTED: true (1) for a[1] ";

cout << "ACTUAL: " << a1[1] << endl;

assert(true == a1[1]);

cout << "EXPECTED: false (0) for a[2] ";

cout << "ACTUAL: " << a1[2] << endl;

assert(false == a1[2]);

a1[-1000] = true;

cout << "EXPECTED: true (1) for a[-1000] ";

cout << "ACTUAL: " << a1[-1000] << endl;

assert(true == a1[1]);

assert(false == a1[2]);

assert(true == a1[-3]);

assert(true == a1[100]);

assert(true != a1[11]);

assert(true != a1[0]);

cout << " Testing the Array::operator[ ] getter ";

const StaticArray b1 = a1;

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

assert(a1[i] == b1[i]);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions