I really need help with my C assignment The following is the

I really need help with my C++ assignment. The following is the information of my question. Thank you for helping me.

Take a close look at the code

// IntCollection class implementation

#include \"IntCollection.h\"

#include <iostream>
using namespace std;

IntCollection::IntCollection()
{
//initialize member data to reflect an empty IntCollection
size = capacity = 0;
data = NULL;
}

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
//
//note: there is a more efficient way to do this using the C
//memory management function realloc(), but that is beyond the
//scope of our course
int *newData;
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;
}

/*
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 class header

#ifndef INTCOLLECTION_H
#define INTCOLLECTION_H

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

class IntCollection
{
private:
int size; // the number of ints currently stored in the int collection
int capacity; // the total number of elements available for stoarge in the data array
int* data; // a pointer to the dynamically allocated data array
void addCapacity(); // a private member function to allocate more memory if necessary
public:
   IntCollection();                                   // constructor
   //~IntCollection();                                   // destructor, to be added!
   //IntCollection(const IntCollection &c);           // copy constructor, to be added!
   void add(int value);
   int get(int index);
   int getSize();
   //IntCollection& operator=(const IntCollection &c);   // to be added!
   //boolean operator==(const IntCollection &c);       // to be added!
   //IntCollection& operator<<(int value);               // to be added!
};

The IntCollection class is a dynamic storage mechanism for storing integers. It could be used in place of an integer array. It allows you to store and retrieve integers using indices.

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)

1. 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.

2. 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 to support 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.

3. 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.

4. 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.

5. 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.

6. 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();

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)

Solution

IntCollection.h file

#ifndef INTCOLLECTION_H
#define INTCOLLECTION_H
const int CHUNK_SIZE=5; // allocate memory in chunks of ints of this size
class IntCollection
{
private:
int size; // the number of ints currently stored in the int collection
int capacity; // the total number of elements available for stoarge in the data array
int* data; // a pointer to the dynamically allocated data array
void addCapacity(); // a private member function to allocate more memory if necessary
public:
IntCollection(); // constructor
~IntCollection(); // destructor, to be added!
IntCollection(const IntCollection &c); // copy constructor, to be added!
   void add(int value);
   int get(int index);
   int getSize();
IntCollection& operator=(const IntCollection &c); // to be added!
bool operator==(const IntCollection &c); // to be added!
//IntCollection& operator<<(IntCollection &a,int value); // to be added!
   friend IntCollection& operator<<(IntCollection& out, int );
};
#endif

-------------------------------------------------------------------------------------------

cpp file

#include \"IntCollection.h\"
#include <iostream>
using namespace std;
IntCollection::IntCollection()
{
//initialize member data to reflect an empty IntCollection
size = capacity = 0;
data = NULL;
}
// destructor added!
IntCollection::~IntCollection()
{
   delete[capacity] data;
}
//copy constructor
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
//
//note: there is a more efficient way to do this using the C
//memory management function realloc(), but that is beyond the
//scope of our course
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;
}

// overloading assignment(=) operator
IntCollection& IntCollection:: operator=(const IntCollection &c)
{
   IntCollection a;

   a.size = c.size;
   a.capacity = c.capacity;

   a.data = new int[a.capacity];
   for (int i = 0; i < c.size; i++)
   {
       a.data[i]= c.data[i];
   }
   return a;

}
//overloading equaltoeualto(==) operator
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;
}

//added overloading << operator
IntCollection& operator<<(IntCollection &a,int value)
{
   a.add(value);

   return a;
}


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;
}
//testing added functions
IntCollection d(c); //test copy constructor
//test if it copied into object d
for (int i = 0; i < c.getSize(); i++)
{
cout << c.get(i) << endl;
}

//test = operator
IntCollection e;
e = c;
cout<<\"test = operator\"<<endl;
//test if e is same as c

for (int i = 0; i < c.getSize(); i++)
{
cout << c.get(i) << endl;
}
//test << operator
IntCollection f;
f<<10<<12<<13;
cout<<\"test << operator\"<<endl;
for (int i = 0; i < f.getSize(); i++)
{
cout << f.get(i) << endl;
}
//test if e and c are equal
cout<<\"test == operator\"<<endl;
//c.add(10);
if( f == c)
{
   cout<<\"two objects are euqal \"<<endl;
}
else
{
   cout<<\"two objects are not euqal \"<<endl;
}

}

I really need help with my C++ assignment. The following is the information of my question. Thank you for helping me. Take a close look at the code // IntCollec
I really need help with my C++ assignment. The following is the information of my question. Thank you for helping me. Take a close look at the code // IntCollec
I really need help with my C++ assignment. The following is the information of my question. Thank you for helping me. Take a close look at the code // IntCollec
I really need help with my C++ assignment. The following is the information of my question. Thank you for helping me. Take a close look at the code // IntCollec
I really need help with my C++ assignment. The following is the information of my question. Thank you for helping me. Take a close look at the code // IntCollec
I really need help with my C++ assignment. The following is the information of my question. Thank you for helping me. Take a close look at the code // IntCollec
I really need help with my C++ assignment. The following is the information of my question. Thank you for helping me. Take a close look at the code // IntCollec

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site