Create a container class named Bag. A bag is a collection of items in no particu
ID: 3638830 • Letter: C
Question
Create a container class named Bag. A bag is a collection of items in no particular order. Items are retrieved from the bag in no particular (random) order.i) Bag: Initialize the bag object. N is the maximum number of values stored in the bag
provided by the client. The default for bag capacity is 10.
Note: The input parameter N can be ignored if it is not needed during the construction
process. But the storage to represent the values in the bag must be obtained
dynamically.
ii). operator+: Adds a value into the bag.
Since the bag has no order, insert the item at either the beginning or the end of the
container.
An example of the call to this operator (assume B2 is a bag of integers):
B2 + 1; // Adds 1 to the bag
iii). getOne: Returns a random number from the bag. Use a random number generator to
determine which element to return.
iv). isEmpty, isFull, and length are the typical functions that are used by container classes.
v). operator>>: reads in bag values from an input file
vi). operator<<: prints the contents of a bag.
Explanation / Answer
Since there is no requiremennt to implement a remove function, I don't implement one.
I get confused that whether the getOne() function should also remove the item in the bag as well? If so, isEmpty(), isFull(), operator+ need to change.
///Bag.h
#include <time.h>
#include <iostream>
using namespace std;
#ifndef BAG_H
#define BAG_H
template <class T>
class Bag
{
private:
int maxSlots;
T *slots;
T *currentSlot;
public:
template <class T> friend ostream &operator<<(ostream &strm, const Bag<T> &rhs);
template <class T> friend istream& operator>>(istream& strm, Bag<T> &lhs);
Bag(int maxSlots = 10);
Bag(const Bag& rhs);
const Bag<T>& operator=(const Bag& rhs);
~Bag() { delete [] slots; }
bool isEmpty() { return currentSlot == slots; }
bool isFull() { return currentSlot - slots == maxSlots; }
int length() { return maxSlots; }
void operator+(const T& rhs) { if (!isFull()) *currentSlot++ = rhs; }
T getOne() const { return slots[rand() % maxSlots]; }
};
//-----------------------------------------------
template <class T>
Bag<T>::Bag(int maxSlots = 10)
:maxSlots(maxSlots)
{
slots = new T[maxSlots];
for (int i = 0; i < maxSlots; i++)
slots[i] = T();
currentSlot = slots;
srand(time(NULL));
}
//-----------------------------------------------
template <class T>
Bag<T>::Bag(const Bag& rhs)
:maxSlots(rhs.maxSlots)
{
slots = new T[maxSlots];
for (int i = 0; i < maxSlots; i++)
slots[i] = rhs.slots[i];
currentSlot = slots;
srand(time(NULL));
}
//-----------------------------------------------
template <class T>
const Bag<T>& Bag<T>::operator=(const Bag& rhs)
{
if (this != &rhs)
{
maxSlots = rhs.maxSlots;
slots = new T[maxSlots];
for (int i = 0; i < maxSlots; i++)
slots[i] = rhs.slots[i];
currentSlot = slots;
}
return *this;
}
//-----------------------------------------------
template <class T>
ostream &operator<<(ostream &strm, const Bag<T> &rhs)
{
for (int i = 0; i < rhs.maxSlots; i++)
strm << rhs.slots[i] << " ";
return strm;
}
//-----------------------------------------------
template <class T>
istream& operator>>(istream& strm, Bag<T> &lhs)
{
T obj = T();
strm >> obj;
lhs + obj;
return strm;
}
//-----------------------------------------------
#endif
///There is no content in Bag.cpp
///TestDriver.cpp
#include <iostream>
#include "Bag.h"
using namespace std;
int main()
{
//LOCAL DECLARATIONS
Bag<int> b1(12);
//PROCEDURES
b1 + 1;
b1 + 2;
b1 + 3;
b1 + 4;
b1 + 5;
b1 + 6;
b1 + 7;
b1 + 8;
b1 + 9;
b1 + 10;
cout << "Enter an int : ";
cin >> b1;
cin.ignore(100, ' ');
cout << b1 << endl;
cout << b1.getOne() << endl;
cout << b1.getOne() << endl;
cout << b1.getOne() << endl;
cout << endl;
cin.get();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.