T This problem is in c++ The following class keeps track of how much of a memory
ID: 3882021 • Letter: T
Question
T
This problem is in c++
The following class keeps track of how much of a memory block (say. 512 bytes) is being used. It contains a pointer that will point to the first element of an array. This array will be of an arbitrary size. There is a second integer to hold the size (as given by the user at the time of instantiation). Each element of the array is a block, and the number kept in that element is the bytes of that block used. class BleckUsageí private: int* blocks: int numBlocss public: // constructors (default, one arg, and copy.) // destructor // Member function ) If this is in the Class Specification file(lackUsageh), write the function definitions that would be in the Class Implementation file (BlockUsage.cpp). For the default constructor, NULL and 0 are appropriate default values. i) Why must the parameter of the copy constructor be of type constant reference?Explanation / Answer
BlockUseage.BlockUseage() {
blocks = NULL;
numBlocks = 0;
}
BlockUseage::BlockUseage(int size) {
blocks = new int[size];
numBlocks = size;
}
BlockUseage::BlockUseage(const BlockUseage& obj) {
blocks = new int[obj.numBlocks];
numBlocks = obj.numBlocks;
for (int i = 0; i < numBlocks; i++)
blocks[i] = obj.blocks[i];
}
BlockUseage::BlockUseage(int size) {
delete[] blocks;
}
int BlockUseage::blockAtIndex(int i) {
return blocks[i]
}
ii. When we create our own copy constructor, we pass an object by reference and we generally pass it as a const reference because in C++ wherever possible so that objects are not accidentally modified. Even the program without const looks fine but it has compiler error. Only if we add const in copy constructor, the program works fine.
I hope that this helped your
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.