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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
#ifndef DMEMORY_H
#define DMEMORY_H
class Dmemory
{
friend void copy(Dmemory *destination, const Dmemory *source);
public:
Dmemory(int size = 100);
~Dmemory();
bool setSize(int newSize);
int getSize() const;
int getValue(int index) const;
bool setValue(int index, int value);
void freeMemory();
private:
int *ptr; // pointer to dynamically allocated array
int size; // current size of the array
};
#endif
//#include "Dmemory.h"
#include <iostream>
using namespace std;
Dmemory::Dmemory(int s)
{
size = s;
ptr = NULL;
cout << "[***PTR***] New Dmemory Object, ptr: " << ptr
<< "; size: " << size << endl;
}
Dmemory::~Dmemory()
{
cout << "[***PTR***] Destroying object, ptr: " << ptr << endl;
}
bool Dmemory::setSize(int newSize)
{
ptr = new int[newSize];
size = newSize;
cout << "[***PTR***] Resize Dmemory Object, ptr: " << ptr <<
"; size: " << size << endl;
if(ptr == 0)
return false; // could not allocate
else
return true;
}
int Dmemory::getSize() const
{
return size;
}
int Dmemory::getValue(int index) const
{
return ptr[index];
}
bool Dmemory::setValue(int index, int value)
{
ptr[index] = value;
return true;
}
void Dmemory::freeMemory()
{
delete [] ptr;
}
void copy(Dmemory *destination, const Dmemory *source)
{
*destination = *source;
cout << "[***PTR***] After copy destination ptr: " << destination->ptr
<< " and source ptr: " << source->ptr << endl;
}
//#include "Dmemory.h"
#include <iostream>
using namespace std;
// function to set all values of a Dmemory object to "value"
void setMemory(Dmemory &x, int value);
// function to verify integrity of Dmemory object
// assumes that all values have been set to "value"
void checkMemory(const Dmemory &x, int value);
int main()
{
int size, index, fillValue, experiment;
int value1, value2;
int i,count;
Dmemory object3(0);
while(true) {
cout << "type experiment type (1-6, 0 to stop): ";
cin >> experiment;
if (experiment == 0) break;
Dmemory object1; // allocate objects each time through the loop
Dmemory object2;
switch (experiment) {
case 1:
cout << "index: ";
cin >> index;
cout << "value: ";
cin >> fillValue;
cout << "value at " << index << " is " << object1.getValue(index) << endl;
object1.setValue(index,fillValue);
cout << "value at " << index << " is " << object1.getValue(index) << endl;
break;
case 2:
cout << "size: ";
cin >> size;
if(!object1.setSize(size)) {
cout << "******** out of space";
return (1);
}
cout << "Object resized" << endl;
cout << "index:";
cin >> index;
cout << "value: ";
cin >> fillValue;
cout << "(Before calling setValue) value at " << index << " is " << object1.getValue(index) << endl;
object1.setValue(index,fillValue);
cout << "(After calling setValue) value at " << index << " is " << object1.getValue(index) << endl;
break;
case 3:
cout << "size: ";
cin >> size;
if(!object1.setSize(size)) {
cout << " out of space";
return (1);
}
if(!object2.setSize(size)) {
cout << "******** out of space";
return (1);
}
cout << "value: ";
cin >> value1;
setMemory(object1, value1);
value2 = value1+10;
setMemory(object2, value2);
cout << "Set value of index which is out of range: ";
cin >> i;
object2.setValue(i,value2);
checkMemory(object1,value1);
break;
case 4:
cout << "size: ";
cin >> size;
if(!object1.setSize(size)) {
cout << " out of space";
return (1);
}
if(!object2.setSize(size)) {
cout << "******** out of space";
return (1);
}
cout << "index:";
cin >> index;
cout << "value: ";
cin >> fillValue;
object1.setValue(index,fillValue);
fillValue = fillValue+10;
object2.setValue(index,fillValue);
cout << "(object1) value at " << index <<
" is " << object1.getValue(index) << endl;
cout << "(object2) value at " << index <<
" is " << object2.getValue(index) << endl;
copy(&object2, &object1);
cout << "after copy\n";
cout << "(object1) value at " << index <<
" is " << object1.getValue(index) << endl;
cout << "(object2) value at " << index <<
" is " << object2.getValue(index) << endl;
fillValue = fillValue+10;
object2.setValue(index,fillValue);
cout << "after changing object 2\n";
cout << "(object1) value at " << index <<
" is " << object1.getValue(index) << endl;
cout << "(object2) value at " << index <<
" is " << object2.getValue(index) << endl;
break;
case 5:
cout << "size: ";
cin >> size;
cout << "index:";
cin >> index;
cout << "starting value: ";
cin >> fillValue;
cout << "count: ";
cin >> count;
for(i = 0; i < count; i++) {
if(!object1.setSize(size)) {
cout << "****** out of space\n";
return (1);
}
object1.setValue(index,fillValue++);
cout << "(object1) value at " << index <<
" is " << object1.getValue(index) << endl;
}
break;
case 6:
cout << "size: ";
cin >> size;
if(!object1.setSize(size)) {
cout << "****** out of space\n";
return (1);
}
cout << "index: ";
cin >> index;
cout << "initial value: ";
cin >> fillValue;
object1.setValue(index, fillValue++);
object1.freeMemory();
if(!object3.setSize(size)) {
cout << "****** out of space\n";
return (1);
}
object3.setValue(index, fillValue);
cout << "(object3) value at " << index <<
" is " << object3.getValue(index) << endl;
object1.setValue(index,fillValue + 1000);
cout << "after setting object1 at " << index << " to " << fillValue + 1000 << endl;
cout << "(object3) value at " << index <<
" is " << object3.getValue(index) << endl;
break;
default:
cout << "******* bad experiment number, reenter:";
}
}
return 0;
}
// function to set all values of a Dmemory object to "value"
void setMemory(Dmemory &x, int value)
{
for(int i = 0; i < x.getSize(); i++) {
x.setValue(i,value);
}
}
// function to verify integrity of Dmemory object
// assumes that all values have been set to "value"
void checkMemory(const Dmemory &x, int value)
{
for(int i=0; i < x.getSize(); i++) {
if(x.getValue(i) != value) {
cout << "corrupted value in object at " << i
<< " value is " << x.getValue(i)
<< " but should be value " << value << endl;
}
}
}
|