#ifndef SMART_ARRAY_H #define SMART_ARRAY_H /* NOTE: * In some of the function i
ID: 3535425 • Letter: #
Question
#ifndef SMART_ARRAY_H
#define SMART_ARRAY_H
/* NOTE:
* In some of the function implementation bodies, you will see partial implementations,
* such as the following (in the capacity() function):
*
size_t result = 0;
// YOUR CODE GOES HERE
return result;
*
* Do not feel obliged to retain any of the supplied code; it is there only to
* get a compilable program for you to start with.
*/
#include <cstring> // for memset
#ifndef size_t
typedef unsigned long size_t;
#endif
template <class T>
class SmartArray
{
// This is the public interface, which all your class's users should
// expect you to honor. How you do that is your own business; in particular,
// any data members and helper functions will be in the private section.
public:
// Constructor. Size defaults to 0, but the user may specify an initial
// size; if a non-zero value is supplied, the array contents are
// initialized to all zeros (see "erase()" for a reusable function).
SmartArray<T>(size_t n = 0);
// Destructor. Since the array will be allocating and deallocating memory,
// it must do so here. If the elements are pointers, it is the
// user's obligation to delete them (since this class has no way of
// knowing whether it's holding pointers, objects, or built-in types.
~SmartArray<T>();
// Return the current capacity of the array; that is, how
// many elements will it hold before it has to resize itself
size_t capacity();
// Return the current size of the array. It is the maximum of all the
// index references since the last clear().
size_t size();
// Boolean test for empty array
bool empty();
// Empty the array and restore it to size = capacity = 0;
void clear();
// Return a pointer to element 0 of the internal data array. The user
// will treat the pointer like a normal array. For example:
//
// Here the user declares a smart array of floats
// SmartArray<float> myArray;
//
// The user does some work to put data into the array, and now the user
// would like to have a normal C++ array, to use like this:
// float ary = myArray.data;
// cout << "The first element is " << ary[0] << endl;
T* data();
// Set every byte of the contents to zero, but do not
// reduce the size of the array.
// Hint: look up the C function "memset"
void erase();
// Your user may want to resize the array on demand.
void resize(const size_t newSize);
// These are overloaded operators; they are member functions, so they
// don't need to be "friends"
// Set one array equal to the other. Note that since T may be a class,
// the "deep" or "shallow" nature of this depends on T's operator=
// implementation.
SmartArray<T>& operator=(const SmartArray<T>& rhs);
// Index into the array. You would use this like:
// SmartArray<float> f;
// f[3] = 3.14;
// You have to be careful that if the supplied index points to an
// invalid memory location, you have to resize the array and copy the
// old to the new. As a rule, if you have to resize, make the new array
// twice as big as is required by this action. The operator returns
// a reference so you can do things like this:
// SmartArray<float> f;
// f[0] = f[1] = 1.0;
T& operator[](const size_t index);
// Return a copy of the first value in the array
T front();
// Push an element onto the back of the array, resizing if needed
void push_back(T newValue);
// Pop an element off the back of the array
void pop_back();
private:
// This is the private section, where you will place all your
// implementation support details.
// This is here only for the compiler to like the operator[] function;
// feel free to replace it
T* myData;
};
// Because of the templatization, the implementation must be available to
// the compiler, so we must include the implementation in the header.
// Constructor. Size defaults to 0, but the user may specify an initial
// size; if a non-zero value is supplied, the array contents are
// initialized to all zeros (see "erase()" for a reusable function).
template <class T>
SmartArray<T>::SmartArray(size_t n)
{
// YOUR CODE GOES HERE
}
// Destructor. Since the array will be allocating and deallocating memory,
// it must do so here. If the elements are pointers, it is the
// user's obligation to delete them (since this class has no way of
// knowing whether it's holding pointers, objects, or built-in types.
template <class T>
SmartArray<T>::~SmartArray()
{
// YOUR CODE GOES HERE
}
// Return the current capacity of the array; that is, how
// many elements will it hold before it has to resize itself
template <class T>
size_t SmartArray<T>::capacity()
{
size_t result = 0;
// YOUR CODE GOES HERE
return result;
}
// Return the current size of the array. It is the maximum of all the
// index references since the last clear().
template <class T>
size_t SmartArray<T>::size()
{
size_t result = 0;
// YOUR CODE GOES HERE
return result;
}
// Empty the array and restore it to capacity = 0;
template <class T>
void SmartArray<T>::clear()
{
// YOUR CODE GOES HERE
}
// Boolean test for empty array
template <class T>
bool SmartArray<T>::empty()
{
// YOUR CODE GOES HERE
return false;
}
// Return a pointer to element 0 of the internal data array. The user
// will treat the pointer like a normal array. For example:
//
// Here the user declares a smart array of floats
// SmartArray<float> myArray;
//
// The user does some work to put data into the array, and now the user
// would like to have a normal C++ array, to use like this:
// float ary = myArray.data;
// cout << "The first element is " << ary[0] << endl;
template <class T>
T* SmartArray<T>::data()
{
// YOUR CODE GOES HERE
return 0;
}
// Set every byte of the contents to a user-supplied value, but do not
// reduce the size of the array.
// Hint: look up the C function "memset"
template <class T>
void SmartArray<T>::erase()
{
// YOUR CODE GOES HERE
}
// Your user may want to resize the array on demand.
template <class T>
void SmartArray<T>::resize(const size_t newCapacity)
{
// YOUR CODE GOES HERE
}
// These are overloaded operators; they are member functions, so they
// don't need to be "friends"
// Set one array equal to the other. Note that since T may be a class,
// the "deep" or "shallow" nature of this depends on T's operator=
// implementation.
template <class T>
SmartArray<T>& SmartArray<T>::operator=(const SmartArray<T>& rhs)
{
// YOUR CODE GOES HERE
return *this;
}
// Index into the array. You would use this like:
// SmartArray<float> f;
// f[3] = 3.14;
// You have to be careful that if the supplied index points to an
// invalid memory location, you have to resize the array and copy the
// old to the new. As a rule, if you have to resize, make the new array
// twice as big as is required by this action. The operator returns
// a reference so you can do things like this:
// SmartArray<float> f;
// f[0] = f[1] = 1.0;
template <class T>
T& SmartArray<T>::operator[](const size_t index)
{
// YOUR CODE GOES HERE
return *myData;
}
// Return a copy of the first value in the array
template <class T>
T SmartArray<T>::front()
{
T result;
// YOUR CODE GOES HERE
return result;
}
// Push an element onto the back of the array, resizing if needed
template <class T>
void SmartArray<T>::push_back(T newValue)
{
// YOUR CODE GOES HERE
}
// Pop an element off the back of the array
template <class T>
void SmartArray<T>::pop_back()
{
// YOUR CODE GOES HERE
}
#endif // SMART_ARRAY_H
Explanation / Answer
// i have implemented the functions body just check it.
#include<iostream>
#ifndef SMART_ARRAY_H
#define SMART_ARRAY_H
/* NOTE:
* In some of the function implementation bodies, you will see partial implementations,
* such as the following (in the capacity() function):
*
size_t result = 0;
// YOUR CODE GOES HERE
return result;
*
* Do not feel obliged to retain any of the supplied code; it is there only to
* get a compilable program for you to start with.
*/
#include <cstring> // for memset
template <class T>
class SmartArray
{
// This is the public interface, which all your class's users should
// expect you to honor. How you do that is your own business; in particular,
// any data members and helper functions will be in the private section.
public:
// Constructor. Size defaults to 0, but the user may specify an initial
// size; if a non-zero value is supplied, the array contents are
// initialized to all zeros (see "erase()" for a reusable function).
SmartArray<T>(size_t n = 0);
// Destructor. Since the array will be allocating and deallocating memory,
// it must do so here. If the elements are pointers, it is the
// user's obligation to delete them (since this class has no way of
// knowing whether it's holding pointers, objects, or built-in types.
~SmartArray<T>();
// Return the current capacity of the array; that is, how
// many elements will it hold before it has to resize itself
size_t capacity();
// Return the current size of the array. It is the maximum of all the
// index references since the last clear().
size_t size();
// Boolean test for empty array
bool empty();
// Empty the array and restore it to size = capacity = 0;
void clear();
// Return a pointer to element 0 of the internal data array. The user
// will treat the pointer like a normal array. For example:
//
// Here the user declares a smart array of floats
// SmartArray<float> myArray;
//
// The user does some work to put data into the array, and now the user
// would like to have a normal C++ array, to use like this:
// float ary = myArray.data;
// cout << "The first element is " << ary[0] << endl;
T* data();
// Set every byte of the contents to zero, but do not
// reduce the size of the array.
// Hint: look up the C function "memset"
void erase();
// Your user may want to resize the array on demand.
void resize(const size_t newSize);
// These are overloaded operators; they are member functions, so they
// don't need to be "friends"
// Set one array equal to the other. Note that since T may be a class,
// the "deep" or "shallow" nature of this depends on T's operator=
// implementation.
SmartArray<T>& operator=(SmartArray<T>& rhs);
// Index into the array. You would use this like:
// SmartArray<float> f;
// f[3] = 3.14;
// You have to be careful that if the supplied index points to an
// invalid memory location, you have to resize the array and copy the
// old to the new. As a rule, if you have to resize, make the new array
// twice as big as is required by this action. The operator returns
// a reference so you can do things like this:
// SmartArray<float> f;
// f[0] = f[1] = 1.0;
T& operator[](const size_t index);
// Return a copy of the first value in the array
T front();
// Push an element onto the back of the array, resizing if needed
void push_back(T newValue);
// Pop an element off the back of the array
void pop_back();
private:
// This is the private section, where you will place all your
// implementation support details.
// This is here only for the compiler to like the operator[] function;
// feel free to replace it
T* myData;
size_t smartArrayCapacity;
size_t smartArraySize;
};
// Because of the templatization, the implementation must be available to
// the compiler, so we must include the implementation in the header.
// Constructor. Size defaults to 0, but the user may specify an initial
// size; if a non-zero value is supplied, the array contents are
// initialized to all zeros (see "erase()" for a reusable function).
template <class T>
SmartArray<T>::SmartArray(size_t n)
{
myData=new T[n];
for(size_t i=0;i<n;i++)
myData[i]=0;
smartArraySize=0;
smartArrayCapacity=n;
}
// Destructor. Since the array will be allocating and deallocating memory,
// it must do so here. If the elements are pointers, it is the
// user's obligation to delete them (since this class has no way of
// knowing whether it's holding pointers, objects, or built-in types.
template <class T>
SmartArray<T>::~SmartArray()
{
delete[] myData;
}
// Return the current capacity of the array; that is, how
// many elements will it hold before it has to resize itself
template <class T>
size_t SmartArray<T>::capacity()
{
return smartArrayCapacity;
}
// Return the current size of the array. It is the maximum of all the
// index references since the last clear().
template <class T>
size_t SmartArray<T>::size()
{
return smartArraySize;
}
// Empty the array and restore it to capacity = 0;
template <class T>
void SmartArray<T>::clear()
{
delete[] myData;
myData=NULL;
smartArrayCapacity=0;
smartArraySize=0;
}
// Boolean test for empty array
template <class T>
bool SmartArray<T>::empty()
{
if(smartArraySize==0)
return true;
else
return false;
}
// Return a pointer to element 0 of the internal data array. The user
// will treat the pointer like a normal array. For example:
//
// Here the user declares a smart array of floats
// SmartArray<float> myArray;
//
// The user does some work to put data into the array, and now the user
// would like to have a normal C++ array, to use like this:
// float ary = myArray.data;
// cout << "The first element is " << ary[0] << endl;
template <class T>
T* SmartArray<T>::data()
{
return mydata;
}
// Set every byte of the contents to a user-supplied value, but do not
// reduce the size of the array.
// Hint: look up the C function "memset"
template <class T>
void SmartArray<T>::erase()
{
for(size_t i=0;i<smartArraySize;i++)
myData[i]=0;
}
// Your user may want to resize the array on demand.
template <class T>
void SmartArray<T>::resize(const size_t newCapacity)
{
T* temp=myData;
if(temp!=NULL)
{
myData=new T[newCapacity];
for(size_t i=0;i<smartArraySize;i++)
myData[i]=temp[i];
for(size_t i=smartArraySize;i<newCapacity;i++)
myData[i]=0;
smartArrayCapacity=newCapacity;
}
else
{
myData=new T[1];
myData[0]=0;
smartArrayCapacity=1;
}
delete[] temp;
}
// These are overloaded operators; they are member functions, so they
// don't need to be "friends"
// Set one array equal to the other. Note that since T may be a class,
// the "deep" or "shallow" nature of this depends on T's operator=
// implementation.
template <class T>
SmartArray<T>& SmartArray<T>::operator=(SmartArray<T>& rhs)
{
resize(rhs.capacity());
for(size_t i=0;i<rhs.size();i++)
myData[i]=rhs[i];
smartArrayCapacity=rhs.capacity();
smartArraySize=rhs.size();
return *this;
}
// Index into the array. You would use this like:
// SmartArray<float> f;
// f[3] = 3.14;
// You have to be careful that if the supplied index points to an
// invalid memory location, you have to resize the array and copy the
// old to the new. As a rule, if you have to resize, make the new array
// twice as big as is required by this action. The operator returns
// a reference so you can do things like this:
// SmartArray<float> f;
// f[0] = f[1] = 1.0;
template <class T>
T& SmartArray<T>::operator[](const size_t index)
{
if(index>=smartArrayCapacity)
resize(2*smartArrayCapacity);
return myData[index];
}
// Return a copy of the first value in the array
template <class T>
T SmartArray<T>::front()
{
if(smartArraySize!=0)
return myData[0];
else
return 0;
}
// Push an element onto the back of the array, resizing if needed
template <class T>
void SmartArray<T>::push_back(T newValue)
{
if(smartArraySize==smartArrayCapacity)
resize(2*smartArrayCapacity);
myData[smartArraySize]=newValue;
smartArraySize++;
}
// Pop an element off the back of the array
template <class T>
void SmartArray<T>::pop_back()
{if(empty()==0)
{
myData[smartArraySize-1]=0;
smartArraySize--;
}
}
#endif // SMART_ARRAY_H
using namespace std;
int main()
{
SmartArray<float> a(10);
a.push_back(10.0);
a.push_back(11.0);
cout<<a.capacity()<<endl;
cout<<a.size()<<endl;
cout<<a.empty()<<endl;;
cout<<a.front()<<endl;
a.pop_back();
cout<<a.front()<<endl;
a.pop_back();
cout<<a.front()<<endl;
a.push_back(14.25);
a.push_back(15.0);
SmartArray<float> b(5);
b=a;
cout<<b.front()<<endl;
cout<<b.size()<<endl;
cout<<b.capacity()<<endl;
b.erase();
cout<<b.front()<<endl;
cout<<b.size()<<endl;
b.clear();
cout<<b.size()<<endl;
cout<<b.capacity()<<endl;
cout<<b.front()<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.