Question: Design a C++ class cCheck that encapsulates an array of numbers and: returns the kth number from the array upon the kth query(), for example:
- Design a C++ class cCheck that encapsulates an array of numbers and:
- returns the kth number from the array upon the kth query(), for example:
If the array is [7, 14, 25, 15]
the first call to query() returns 7, the second call returns 14,
- when the last number in the array is returned from query()
resets the query count
expands the array: e.g. [7, 14, 25, 15] becomes, say, [7, 14, 25, 15, 35]
- supports replacing the encapsulated array only if the replacement is not smaller
- supports efficient, deep copying
Define this class, providing all implementation details.
You must use arrays and may not use another container (such as vector).
public class cCheck {
private int[] arr;
private const int DEFAULT_SIZE = 10;
private const int ARRAY_MULTIPLIER = 5;
private uint queryCount;
public cCheck() {
queryCount = 0;
arr = new int[DEFAULT_SIZE];
for (int k = 0; k < DEFAULT_SIZE; k++) {
arr[k] = (k+1) * ARRAY_MULTIPLIER;
}
}
public int query() {
if (queryCount == arr.length()) {
queryCount = 0;
arr = new int[arr.length() * 2];
for (int k = 0; k < arr.length(); k++) {
arr[k] = (k+1) * ARRAY_MULTIPLIER;
}
}
int save = arr[queryCount];
queryCount++;
return save;
}
public void replace(int[] x) {
if (x.length() > arr.length()) {
delete[] arr;
arr = new int[x.length()];
for (int k = 0; k < arr.length(); k++) {
arr[k] = x[k];
}
}
}
public void copy(const cCheck& src) {
arr = new int[src.length()]
for (int k = 0; k < src.length(); k++) {
arr[k] = src.arr[k];
}
queryCount = src.queryCount;
}
public void operator=(const cCheck& src) {
if (this == &src) return;
delete[] arr;
copy(src);
return *this;
}
public ~cCheck() {
delete[] arr;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
