Implement the bag type (.cpp file) and add to main.cxx to check all the function
ID: 3710554 • Letter: I
Question
Implement the bag type (.cpp file) and add to main.cxx to check all the functions!
https://drive.google.com/drive/u/0/folders/1ymO6Q1MU2f_2a1MmsjrgFONivmQu_N70
look at the .h file and see what we need to implement
A. Private Variables
value_type *data; size_type used; size_type capacity;
B. Implement Constructors and Destructors
bag(size_type initial_capacity= DEFAULT_CAPACITY)
You need to create *data to point to a dynamic array (using the new) of
value_type with initial_capacity. Set used and capacity accordingly bag(const bag& source)
Copy constructor. make sure you do a DEEP COPY meaning you create another dynamic array and set the array of our current bag instance to the array of the source instance. You also have to set used and capacity equal.
~bag(); //destructor
Destructor must free the dynamic memory allocated using delete command
C. Insert
void insert(const value_type& entry); //inserts into your bag
Insert the value into your dynamic array. Note that if you are at capacity, you need to reserve more space (i.e. make your bag bigger!).
D. Reserve
void reserve(size_type new_capacity);
Reserve is our first private function— We can make reserve a private function because it’s only called from other member functions (i.e. it shouldn’t be called from main). Make a new dynamic array with a greater capacity than your current bag. Then copy over your old bag into your new bag and then DELETE your old bag.
E. Erase_one and Erase
bool erase_one (const value_type& target);//erase 1 of the target value
erase 1 deletes 1 of the target value in the bag (in this case it returns true). Or it returns false if none exist in the bag
//if bag a has 1 2 3 4 in it
a.erase_one(3)
//bag a now has 1 2 4 in it.
size_type erase(const value_type& target)
erases all the values of target in the bag. //if bag a has 2 2 3 4 in it
a.erase(2)
//bag a now has 3 4 in it
You should call erase_one from erase
F. Operator+= and Operator=
void operator +=(const bag& addend);
Operator += will do the following
//lets’ say bag a has 1 2 3 and bag b has 4 5 6
a+=b;
//now bag a has 1 2 3 4 5 6, you can use insert here
void operator =(const bag& addend);
//let’s say bag a has 1 2 3 and bag b has 2 3 4 5 6 a=b
//now bag a has 2 3 4 5 6
G. Size, Count, cout<<
size_type size() const {return used;}
size_type count (const value_type& target) const;
friend std::ostream& operator <<(std::ostream& outs, const bag& source);
Cout and size are implemented for you.
count takes in a target and gives you how many of a given type are in your instance
//if bag a has 4 5 3 4
cout<<a.count(4)
//2 will be coutted
Explanation / Answer
Given below is the code for bag.cpp. Use it along with the bag.h and test program given by instructor
Please do rate the answer if it was helpful. Thank you
bag.cpp
=======
#include "bag.h"
#include <iostream>
using namespace std;
bag::bag(size_type initial_capacity ) //constructor with default arguments
{
used = 0;
capacity = initial_capacity;
data = new value_type[capacity];
}
bag::bag(const bag& source) //copy constructor
{
used = source.used;
capacity = source.capacity;
data = new value_type[capacity];
for(int i= 0; i < used; i++)
data[i] = source.data[i];
}
bag::~bag() //destructor
{
delete[] data;
}
void bag::insert(const value_type& entry) //inserts into your bag
{
if(used == capacity)
reserve(2*capacity);
data[used++] = entry;
}
bool bag::erase_one(const value_type& target)//erase 1 of the target value
{
for(int i = 0; i < used; i++)
{
if(data[i] == target)
{
data[i] = data[used-1]; //store the last value in the array in place of erased item
used--;
return true;
}
}
return false;
}
bag::size_type bag::erase(const value_type& target)//erase all of the target value in the bag
{
size_type count = 0;
for(int i = 0; i < used; i++)
{
if(data[i] == target)
{
data[i] = data[used-1]; //store the last value in the array in place of erased item
used--;
count++;
}
}
return count;
}
void bag::operator +=(const bag& addend) //overloades += remember if x=5 x+=10 makes x=15. Similarly for bag...
{
for(int i = 0; i < addend.used; i++)
insert(addend.data[i]);
}
void bag::operator =(const bag& source)//sets two bags equal to each other
{
if(&source != this) //avoid self assignement
{
delete[] data;
used = source.used;
capacity = source.capacity;
data = new value_type[capacity];
for(int i= 0; i < used; i++)
data[i] = source.data[i];
}
}
bag::size_type bag::count(const value_type& target) const //returns how much of a given value you have inside the bag
{
size_type count = 0;
for(int i = 0; i < used; i++)
{
if(data[i] == target)
count++;
}
return count;
}
void bag::reserve(size_type new_capacity) //make your bag bigger
{
capacity = new_capacity;
value_type *temp = new value_type[capacity];
for(int i = 0; i < used; i++)
temp[i] = data[i];
delete[] data;
data = temp;
}
std::ostream& operator <<(std::ostream& outs, const bag& source)//couts the bag contents, if bag contains 1 2 and 3 format should be "Bag contains: 1,2,3"
{
outs << "Bag contains: ";
if(source.used > 0)
{
outs << source.data[0];
for(int i = 1; i < source.used;i++)
outs << ", " << source.data[i];
}
outs << endl;
return outs;
}
output
=====
$ ./a.out
**IMPORTANT This testing script relies on Size and COUNT ,
if you did not implement count correctly, do that before testing
size is implemented for you
LATER TESTS BASED ON EARLIER TESTS so go in order
Destructor and memory leaks will be hand graded, and be 10 percent
Pick what you would like to test
1- Default Constructor
2- Insert Test
3- Copy Constructor
4- Reserve Test
5- Erase One Test
6- Erase (all) Test
7- Operator Equal Overlaod
8- Plus Equals Test Overload
9- Cout Test
1
Default constructor test passed!
$ ./a.out
**IMPORTANT This testing script relies on Size and COUNT ,
if you did not implement count correctly, do that before testing
size is implemented for you
LATER TESTS BASED ON EARLIER TESTS so go in order
Destructor and memory leaks will be hand graded, and be 10 percent
Pick what you would like to test
1- Default Constructor
2- Insert Test
3- Copy Constructor
4- Reserve Test
5- Erase One Test
6- Erase (all) Test
7- Operator Equal Overlaod
8- Plus Equals Test Overload
9- Cout Test
2
Insert test passed!
$ ./a.out
**IMPORTANT This testing script relies on Size and COUNT ,
if you did not implement count correctly, do that before testing
size is implemented for you
LATER TESTS BASED ON EARLIER TESTS so go in order
Destructor and memory leaks will be hand graded, and be 10 percent
Pick what you would like to test
1- Default Constructor
2- Insert Test
3- Copy Constructor
4- Reserve Test
5- Erase One Test
6- Erase (all) Test
7- Operator Equal Overlaod
8- Plus Equals Test Overload
9- Cout Test
3
Copy Constructor Test passed!
$ ./a.out
**IMPORTANT This testing script relies on Size and COUNT ,
if you did not implement count correctly, do that before testing
size is implemented for you
LATER TESTS BASED ON EARLIER TESTS so go in order
Destructor and memory leaks will be hand graded, and be 10 percent
Pick what you would like to test
1- Default Constructor
2- Insert Test
3- Copy Constructor
4- Reserve Test
5- Erase One Test
6- Erase (all) Test
7- Operator Equal Overlaod
8- Plus Equals Test Overload
9- Cout Test
4
Got to here
Reserve Test works!
$ ./a.out
**IMPORTANT This testing script relies on Size and COUNT ,
if you did not implement count correctly, do that before testing
size is implemented for you
LATER TESTS BASED ON EARLIER TESTS so go in order
Destructor and memory leaks will be hand graded, and be 10 percent
Pick what you would like to test
1- Default Constructor
2- Insert Test
3- Copy Constructor
4- Reserve Test
5- Erase One Test
6- Erase (all) Test
7- Operator Equal Overlaod
8- Plus Equals Test Overload
9- Cout Test
4
Got to here
Reserve Test works!
$ ./a.out
**IMPORTANT This testing script relies on Size and COUNT ,
if you did not implement count correctly, do that before testing
size is implemented for you
LATER TESTS BASED ON EARLIER TESTS so go in order
Destructor and memory leaks will be hand graded, and be 10 percent
Pick what you would like to test
1- Default Constructor
2- Insert Test
3- Copy Constructor
4- Reserve Test
5- Erase One Test
6- Erase (all) Test
7- Operator Equal Overlaod
8- Plus Equals Test Overload
9- Cout Test
5
Erase One Passed!
$ ./a.out
**IMPORTANT This testing script relies on Size and COUNT ,
if you did not implement count correctly, do that before testing
size is implemented for you
LATER TESTS BASED ON EARLIER TESTS so go in order
Destructor and memory leaks will be hand graded, and be 10 percent
Pick what you would like to test
1- Default Constructor
2- Insert Test
3- Copy Constructor
4- Reserve Test
5- Erase One Test
6- Erase (all) Test
7- Operator Equal Overlaod
8- Plus Equals Test Overload
9- Cout Test
6
Erase Test Failed!
$ ./a.out
**IMPORTANT This testing script relies on Size and COUNT ,
if you did not implement count correctly, do that before testing
size is implemented for you
LATER TESTS BASED ON EARLIER TESTS so go in order
Destructor and memory leaks will be hand graded, and be 10 percent
Pick what you would like to test
1- Default Constructor
2- Insert Test
3- Copy Constructor
4- Reserve Test
5- Erase One Test
6- Erase (all) Test
7- Operator Equal Overlaod
8- Plus Equals Test Overload
9- Cout Test
7
Operator Equals Test passed!
$ ./a.out
**IMPORTANT This testing script relies on Size and COUNT ,
if you did not implement count correctly, do that before testing
size is implemented for you
LATER TESTS BASED ON EARLIER TESTS so go in order
Destructor and memory leaks will be hand graded, and be 10 percent
Pick what you would like to test
1- Default Constructor
2- Insert Test
3- Copy Constructor
4- Reserve Test
5- Erase One Test
6- Erase (all) Test
7- Operator Equal Overlaod
8- Plus Equals Test Overload
9- Cout Test
8
Operator += Test Passed
$ ./a.out
**IMPORTANT This testing script relies on Size and COUNT ,
if you did not implement count correctly, do that before testing
size is implemented for you
LATER TESTS BASED ON EARLIER TESTS so go in order
Destructor and memory leaks will be hand graded, and be 10 percent
Pick what you would like to test
1- Default Constructor
2- Insert Test
3- Copy Constructor
4- Reserve Test
5- Erase One Test
6- Erase (all) Test
7- Operator Equal Overlaod
8- Plus Equals Test Overload
9- Cout Test
9
Cout Test, should print out numbers 0 to 99
Bag contains: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99