C++ why is my output only 1 , 3 , 4 when i just want to remove 55.
code:
template
class Collection{
Objects *array; // pointer dynamically allocate array
int currentSize;
int capacity;
public:
// Constructor
Collection(int capacity) : capacity(capacity){
array = new Objects[capacity];
}
// deconstructor to avoid memory leakage
~Collection(){
delete[] array;
}
// checks if collection is empty
bool isEmpty()const {
return currentSize == 0;
}
int makeEmpty(){
return currentSize = 0;
}
// add more elements to the array if enough capacity.
void insert(const Objects& n){
if(currentSize < capacity)
array[currentSize++] = n;
else
std::cout << "Collection is full to enter another element" << std::endl;
}
// remove element from array.... but it doesn't work yet
void remove(const Objects& index){
for(int i = 0; i < currentSize; ++i){
if(array[i] == index){
for(int j = i; j < currentSize - 1; ++j){
array[j] = array[j + 1];
}
}
currentSize--;
}
}
// this functin will search the arrray if it contains specify number
//void contains(){}
// prints the entire array
void print() {
std::cout << "Collection contents: " << std::endl;
for(int i = 0; i < currentSize; ++i){
std::cout << array[i] << std::endl;
}
}
};
int main(){
Collection Collection(6);
Collection.insert(1);
Collection.insert(3);
Collection.insert(4);
Collection.insert(55);
Collection.insert(23);
Collection.insert(9);
int removeNum = 55;
Collection.remove(removeNum);
Collection.print();
return 0;
}



Answer :

Other Questions