1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
// Container with dynamic storage
#include <iostream>
#include <iomanip>
using namespace std;
class container {
friend ostream& operator<<(ostream& out, container &);
// Postcondition: displays # of values stored in the container, storage capacity of the contianer, and stored values in the container
// in the following format: Array size = 3, capacity = 4, contents = 11, 22, 33 (see below sample program output
public:
container();
// Postcondition: set dynamic storage capacity to 1 and count to -1 where (count + 1) represents the actual values stored
// in the container. Notice that data member count is used as the subscript to access elements (actual values) stored
// in the dynamic array; thus (count + 1) represents the total # of values that are currently stored in the array
container(int n);
// Postcondition: set dynamic storage (data array) capacity to n and count to -1
container(container &obj);
// Programmer-supplied copy constructor is necessary to avoid memory leak and other side effect
// Postcondition: a new container class object is created which is the same as the one passed to the function
~container();
// Programmer-supplied destructor is necessary to avoid memory leak
// Postcondition: all dynamic memory locations have been returned back to the heap whenever a container object goes out of scope
container& operator=(const container &rhs);
// Programmer-supplied overloaded assignment is necessary to avoid memory leak and other side effect
// Postconditoin: the container object rhs is assigned to the calling object
void insert(int value);
// Postcondition: if the container is not full, the value passed to the function is stored in
// the first available element of the dynamic array. Otherwise the function calls the private
// "allocate" member function requesting a new set of dynamic memory with twice the previous storage capacity
// the insert function then increments count by 1 and insert the value into the new and larger array.
void remove();
// Precondition: the data array must not be empty; i.e., count must be greater than or equal to 0.
// Postcondition: if the container is not empty, then remove the most recently stored value ifrom the container and
// decrement count by 1; otherwise, display the message "The container is empty; no action is taken!"
int operator[](int sub);
// Precondition: value passed to the function must be a positive integer including 0
// Postcondition: the value of stored in data[sub] is returned; if sub is out of range, display a message and terminate the program .
bool isFull();
// Postcondition: return true if the container is full; return false otherwise
bool isEmpty();
// Postcondition: return true if the container is empty; return false otherwise
int Capacity();
// Notice uppercase 'C' to avoid conflict with data member named "capacity"
// Postcondition: returns the current storage capacity of the container
int size();
// Postcondition: returns the # of elements (# of objects) currently stored in the container
void resize(int n);
// Postcondition: container (i.e., the dynamic array) is resized to n; contents of existing container have been copied to the new array;
// old array is deleted to avoid memory leak.
private:
void allocate();
// Postcondition: 1) the capacity of the container has been doubled, 2) existing values in the existing array have been copied to
// the new and larger dynamic array, 3) memory of the old array has been deleted (returned to "heap").
int *data;
static int capacity=4; // indicates the storage capcity of the container, i.e., the size of the dynamic array
int count; // used as a subscript to index into the array; size = count + 1
};
container::container()
{
capacity =1;
data = new int[capacity];
count = -1;
}
container::container(int)
{
capacity=n;
data=new int[capacity];
count =-1;
}
container::container(container & obj)
{
data=new int[capacity];
for(int i=0;i<capacity;i++)
data[i]=obj.data[i];
count = obj.count;
}
container::~container()
{
delete[]data;
}
const container & container::operator=(const container &rhs)
{
if(this!=&rhs)
{
if(capacity!=rhs.capacity)
{
delete[] data;
capacity = rhs.capacity;
count=new int [capacity];
}
for(int i=0;i<capacity;i++)
data[i]=obj.data[i];
count=rhs.count;
}
}
void container::instert(int value)
{
if(isFull())
{
cout<<endl<<"container is full";
exit(1);
}
count++;
data[count]=value
}
void container::remove() // logical removal
{
if( isEmpty( ) ) // the stack is empty
{
cout << endl << "Stack is empty" << endl;
exit( 1 );
}
--count;
}
int container::operator[](int sub)
{
int num;
if(sub<0)
{
cout<<"sub is out of range";
exit(1);
}
number=data[sub];
return number;
}
bool container::isEmpty()
{
return( count == -1 );
}
bool container::isFull()
{
return( count == CAPACITY -1 );
}
int container::Capacity()
{
return capacity;
}
int container::size()
{
return count;
}
void container::resize()
{
int resize = size*2;
int *newsize = new int[resize];
size=newsize;
delete [] data;
data = newsize;
}
ostream& operator<<(ostream& out, container & obj)
{
out << "Array size = "<<obj.size();
out<< "capacity = "<<obj.Capacity();
out<<"contents = ";
if (obj.count == -1)
out << "*** data array is empty!" << endl;
else
{
for (int i = 0; i <= obj.count; i++)
out << obj.data[i] << '\t';
out << endl;
}
return out;
}
|