Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Is my code C++ Correct? The IntCollection class is a dynamic storage mechanism f

ID: 3716935 • Letter: I

Question

Is my code C++ Correct?

The IntCollection class is a dynamic storage mechanism for storing integers. It could be used in place of an integer array. Like arrays, it allows you to store and retrieve integers using indices in square brackets [ ].

The following code creates an IntCollection object named ‘c’. It adds seven integers to ‘c’, then uses a for loop to display the seven integers:

int main()

{

IntCollection c;

c.add(45);

c.add(-210);

c.add(77);

c.add(2);

c.add(-21);

c.add(42);

c.add(7);

for (int i = 0; i < c.getSize(); i++)

{

  cout << c.get(i) << endl;

}

}

For this assignment you will add a copy constructor, a destructor, and three overloaded operators to the IntCollection class. In the design diagram below, the black member functions represent code that has already been implemented. You will be implementing the green items. Each item that you will be adding to the class is described below the diagram.

Private:

  int size // the number of ints currently stored in the int collection

  int capacity // total number of elements available in data array

  int* data   // a pointer to the dynamically allocated data array

void addCapacity(); // private function to allocate more memory if necessary

Public:

  IntCollection()

  ~IntCollection()

  IntCollection(const IntCollection &c)

void add(int value)

  int get(int index)

  int getSize()

  IntCollection& operator=(const IntCollection &c)

bool operator==(const IntCollection &c)

  IntCollection& operator<<(int value)

The Copy Constructor. The copy constructor should perform a deep copy of the argument object, i.e. it should construct an IntCollection with the same size and capacity as the argument, with its own complete copy of the argument's data array.

The Assignment Operator (=). The assignment operator should also perform a deep copy of the argument object. It must return itself (or more efficiently, a reference to itself) in order tosupport multiple assignments on the same line, e.g. a = b = c. If you implement your assignment operator first it could be used in the copy constructor, but this is not a requirement.

The Is Equals operator (==). The "is equals" operator should return true if the argument object has the same size as the receiving object, and the values in both objects’ data arrays are identical.

The insertion operator (<<). The insertion operator should add the int parameter into the receiving IntCollection. The functionality is exactly the same as the add() function, i.e. add ints to the collection. Note, however, that this function must return a reference to itself in order to support multiple insertions on the same line, e.g. c << 45 << -210. Unlike the assignment operator, this return must be done by reference, because each insertion actually modifies the IntCollection object, and insertion is done from left to right.

The destructor. Function add() calls addCapacity() to allocate memory when it needs more room. Nowhere in this program is the memory deallocated with delete [], which means we have a memory leak! Add a destructor which correctly handles this.

addCapacity.  Note that addCapacity() is a private member function. What happens if you try to call it from outside the class, i.e. by adding the line below to main()?

c.addCapacity();

You should test the new constructor and operators to your own satisfaction using a test program, e.g. a main() function in main.cpp. Make sure your tests demonstrate all of the required functionality including multiple assignments on the same line and multiple insertions on the same line. Paste sample output showing your test results at the bottom of your main program file main.cpp. In addition to sample output, include an answer to question 6 above.

This is the code I have so far but it isn't working. I really need the help thank you.

IntCollection.cpp:

#include "IntCollection.h"

#include

using namespace std;

IntCollection::IntCollection()

{

// Initialize member data to reflect an empty

// IntCollection

size = capacity = 0;

data = NULL;

}

IntCollection::~IntCollection()

{

delete [] data;

}

IntCollection::IntCollection(const IntCollection &c) {

size = c.size;

capacity = c.capacity;

data = new int[capacity];

for (int i = 0; i < c.size; i++)

{

data[i] = c.data[i];

}

}

void IntCollection::addCapacity()

{

// Create a new, bigger buffer, copy the current data to

// it, delete the old buffer, and point our data

// pointer to the new buffer

int *newData;

data = new int[capacity];

capacity += CHUNK_SIZE;

newData = new int[capacity];

for(int i = 0; i < size; i++)

{

newData[i] = data[i];

delete [] data;

data = newData;

}

}

void IntCollection::add(int value)

{

// first, allocate more memory if we need to

if(size == capacity)

{

addCapacity();

}

// Now, add the data to our array and increment size

data[size++] = value;

}

int IntCollection::get(int index)

{

if (index < 0 || index >= size)

{

cout << "ERROR: get() trying to access index out of range. ";

exit(1);

}

return data[index];

}

int IntCollection::getSize()

{

return size;

}

IntCollection &IntCollection::operator=(const IntCollection &c)

{

IntCollection b;

  

b.size = c.size;

b.capacity = c.capacity;

b.data = new int[b.capacity];

for(int i = 0; i < c.size; i++)

{

b.data[i] = c.data[i];

}

return b;

}

bool IntCollection::operator==(const IntCollection &c)

{

if((size == c.size) && (capacity == c.capacity))

{

for (int i = 0; i < size; i++)

{

if (data[i] == c.data[i])

{

continue;

}

else

{

return false;

}

}

}

return true;

}

IntCollection &IntCollection::operator<<(int value)

{

add(value);

return *this;

}

IntCollection.h:

#ifndef INTCOLLECTION_H
#define INTCOLLECTION_H

// Allocate memory in chunks of ints of this size.
const int CHUNK_SIZE = 5;

class IntCollection
{
private:
// The number of ints currently stored in the int
int size;
// the total number of elements available for storage
// in the data array
int capacity;
// A pointer to the dynamically allocated data array
int* data;
// a private member function to allocate more memory
// if necessary
void addCapacity();
public:
// Constructor
IntCollection();
// Destructor
~IntCollection();
// Copy constructor:
IntCollection(const IntCollection &c);
  
void add(int value);
int get(int index);
int getSize();
IntCollection& operator=(const IntCollection &c);
bool operator==(const IntCollection &c);
IntCollection& operator<<(int value);
};

#endif

main.cpp:

#include "IntCollection.h"
#include
using namespace std;

int main()
{
IntCollection c;
  
c.add(45);
c.add(-210);
c.add(77);
c.add(2);
c.add(-21);
c.add(42);
c.add(7);
  
for (int i = 0; i < c.getSize(); i++)
{
cout << c.get(i) << endl;
}
  
IntCollection d(c);
for (int i = 0; i < c.getSize(); i++)
{
cout << c.get(i) << endl;
}
  
IntCollection e;
e = c;
cout << "Testing = Overload" << endl;
  
for(int i = 0; i < c.getSize(); i++)
{
cout << c.get(i) << endl;
}
  
IntCollection f;
f<<8<<9<<10;
cout << "Testing< for(int i = 0; i < f.getSize(); i++)
{
cout << f.get(i) << endl;
}
  
cout << "Testing == Overload" << endl;
  
c.add(10);
  
if(f == c)
{
cout << "Both objects match" << endl;
}
else
{
cout << "They don't match" << endl;
}
  
return 0;
}

Private:

  int size // the number of ints currently stored in the int collection

  int capacity // total number of elements available in data array

  int* data   // a pointer to the dynamically allocated data array

void addCapacity(); // private function to allocate more memory if necessary

Public:

  IntCollection()

  ~IntCollection()

  IntCollection(const IntCollection &c)

void add(int value)

  int get(int index)

  int getSize()

  IntCollection& operator=(const IntCollection &c)

bool operator==(const IntCollection &c)

  IntCollection& operator<<(int value)

Explanation / Answer

// I HAVE FOUND SOME BUGS IN THE CODE WHICH I HAVE FIXED. SEE MY COMMENTS (LIKE wrong)
// IntCollection.h

#ifndef INTCOLLECTION_H
#define INTCOLLECTION_H

// Allocate memory in chunks of ints of this size.
const int CHUNK_SIZE = 5;

class IntCollection
{
private:
// The number of ints currently stored in the int
int size;
// the total number of elements available for storage
// in the data array
int capacity;
// A pointer to the dynamically allocated data array
int* data;
// a private member function to allocate more memory
// if necessary
void addCapacity();
public:
// Constructor
IntCollection();
// Destructor
~IntCollection();
// Copy constructor:
IntCollection(const IntCollection &c);
  
void add(int value);
int get(int index);
int getSize();
IntCollection& operator=(const IntCollection &c);
bool operator==(const IntCollection &c);
IntCollection& operator<<(int value);
};

#endif


// IntCollection.cpp

#include "IntCollection.h"
#include <iostream>
using namespace std;

IntCollection::IntCollection()

{

// Initialize member data to reflect an empty

// IntCollection

// It was wrong , should be initalize all the values correctly

size = 0;

capacity = CHUNK_SIZE;

data = new int[capacity];

}

IntCollection::~IntCollection()

{

delete[] data;

}

IntCollection::IntCollection(const IntCollection &c) {

size = c.size;

capacity = c.capacity;

data = new int[capacity];

for (int i = 0; i < c.size; i++)
{

data[i] = c.data[i];

}

}

void IntCollection::addCapacity()

{

// Create a new, bigger buffer, copy the current data to

// it, delete the old buffer, and point our data

// pointer to the new buffer

int *newData;

//data = new int[capacity]; // wrong , This action will loose all the data.

capacity += CHUNK_SIZE;

newData = new int[capacity];

for(int i = 0; i < size; i++)

{

newData[i] = data[i];

}

delete[] data; // wrong, Both these statement was inside the for loop
data = newData;
}

void IntCollection::add(int value)

{

// first, allocate more memory if we need to

if(size == capacity)

{

addCapacity();

}

// Now, add the data to our array and increment size

data[size++] = value;

}

int IntCollection::get(int index)

{

if (index < 0 || index >= size)

{

cout << "ERROR: get() trying to access index out of range. ";

exit(1);

}

return data[index];

}

int IntCollection::getSize()

{

return size;

}

IntCollection& IntCollection::operator=(const IntCollection &c)

{

//IntCollection b; // wrong, this should be not be there
  
size = c.size;

capacity = c.capacity;

data = new int[capacity];

for(int i = 0; i < c.size; i++)
{

data[i] = c.data[i];

}

return *this;

}

bool IntCollection::operator==(const IntCollection &c)

{

if((size == c.size) && (capacity == c.capacity))

{

for (int i = 0; i < size; i++)

{

if (data[i] == c.data[i])
{

continue;

}
else
{

return false;

}

}

return true;

} // changed what if size and capacities are not equal
else
{
return false;
}

}

IntCollection &IntCollection::operator<<(int value)

{

add(value);

return *this;

}

// main.cpp

#include <iostream>
#include "IntCollection.h"

using namespace std;

int main()
{
IntCollection c;
  
c.add(45);
c.add(-210);
c.add(77);
c.add(2);
c.add(-21);
c.add(42);
c.add(7);
  
for (int i = 0; i < c.getSize(); i++)
{
cout << c.get(i) << endl;
}
  
cout << "Testing = Copy constructor" << endl;  
IntCollection d(c);
for (int i = 0; i < d.getSize(); i++) //wrong, Now use for d not for c object
{
cout << d.get(i) << endl;
}
  
cout << "Testing = Overload" << endl;  
IntCollection e;
e = c;
  
for(int i = 0; i < e.getSize(); i++) //wrong, Now use for e not for c object
{
cout << e.get(i) << endl;
}
  
IntCollection f;
f<<8<<9<<10;
cout << "Testing << "<<endl;
for(int i = 0; i < f.getSize(); i++)
{
cout << f.get(i) << endl;
}
  
cout << "Testing == Overload" << endl;
  
c.add(10);
  
if(f == c)
{
cout << "Both objects match" << endl;
}
else
{
cout << "They don't match" << endl;
}
  
return 0;
}


//PLEASE PROVIDE FEEDBACK THUMBS UP

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote