Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C + + why is my output only 1 , 3 , 4 when i just want to remove 5 5 . code: template class

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;
}
Thank you in advance!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

5. We have often heard caregivers tell their children?

Answered: 1 week ago

Question

9. Describe the characteristics of power.

Answered: 1 week ago