Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'd like to output capacity for test drivers, but somehow it's compile error. And the instructor says: Include a public getter named DynamicArray: :capacity ()

I'd like to output capacity for test drivers, but somehow it's compile error.

And the instructor says:

Include a public getter named DynamicArray::capacity() to return the data structure's now-variable capacity.

Include a public setter named DynamicArray::capacity(int) to change the capacity.

I don't even know if I have these function already.

==============================================

#ifndef DynamicArray_h #define DynamicArray_h

template class DynamicArray { T *values; int cap; T dummy=T(); public: DynamicArray(int=2); // constructor // default=2 DynamicArray(const DynamicArray&); // copy constructor ~DynamicArray(){ delete [] values;} // destructor DynamicArray& operator=(const DynamicArray&); // assignment operator int capacity() const; void capacity(int); // setter T operator[](int) const; // getter version T& operator[](int); // setter version };

template DynamicArray::DynamicArray(int cap) // constructor { dummy=T(); values=new T[cap]; this->cap=cap; for(int i=0; i

template DynamicArray::DynamicArray(const DynamicArray& original) // copy constructor { cap=original.cap; values=new T[cap]; for(int i=0; i

template DynamicArray& DynamicArray::operator=(const DynamicArray& original) { if(this!=&original) { delete [] values; cap=original.cap; values=new T[cap]; for(int i=0; i

template void DynamicArray::capacity(int cap) { T *temp=new T[cap]; int limit=(capcap ?cap: this->cap); for(int i=0; icap=cap; }

template T DynamicArray::operator[](int index) const // getter { if(index<0) return dummy; if(index>=cap) capacity(2*index); return values[index]; }

template T& DynamicArray::operator[](int index) // setter { if(index<0) return dummy; if(index>=cap) capacity(2*index); return values[index]; }

#endif

==============================================

#include #include

using namespace std;

#include

#include "DynamicArray.h" #include "DynamicArray.h" // test

int main() { cout << "Programmer's name: Seita Fujiwara "; cout << "Programmer's ID: 1741873 "; cout << "file: " << __FILE__ << " "; // 1 Test all public functions cout << "============================================ "; DynamicArray a(100); cout << "Int DynamicArray Test: "; cout << "============================================ "; cout << "Testing DynamicArray::DynamicArray "; // constractor test for(int i=0; i<100; i++) assert(a[i]==0); cout << "Pass! "; cout << "Testing DynamicArray::capacity "; cout << "Expected: 100 "; /* cout << "ACTUAL: " << a.capacity(); assert(a.capacity()==100);*/ //StaticArray::operator[]setter cout << " Testing the DynamicArray::operator[]setter "; a[10]=1; cout << "Expected: 1 for a[10] "; cout << "ACTUAL: " << a[10] << endl; assert(a[10]==1); a[-1000]=666; cout << " Expected: 666 for a[-1000] (dummy) "; cout << "ACTUAL: " << a[-100] << endl; assert(a[-100]==666); a[300]=10; cout << "Expected: the new capacity for a is 200*2 "; /* cout << "ACTUAL: " << iArray.capacity() << endl; */

cout << " Testing the DynamicArray::operator[]getter "; DynamicArray a2 = a; for(int i=0; i<100; i++) assert(a[i]==a2[i]); cout << "Pass! ";

// 2 Const object test // 3 Object copy test /* cout << " Object copy test "; DynamicArray iArray2=iArray; assert(iArray.capacity()==iArray2.capacity()); for(int i=0; i assert(iArray[i]==iArray2[i]); for(int i=0; i { iArray[i]++; assert(iArray[i]!=iArray2[i]); }*/ // Object assignment test return 0; }

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

More Books

Students also viewed these Databases questions

Question

What is released by an exothermic reaction?

Answered: 1 week ago