I want to Update the Bag class to be a template class that can use any type. Use
ID: 672434 • Letter: I
Question
I want to Update the Bag class to be a template class that can use any type. Use the slides from 101 and the LinkedList class provided as an example. The public interface for Bag shouldn't change, with the exception of now having to specify the type at creation.
CSCI2421::Bag<int> bag;
//// Bag.h
#ifndef PA5_BAG_H
#define PA5_BAG_H
#include <cstdlib>
#include <ostream>
namespace CSCI2421
{
class Bag
{
public:
typedef int value_type;
// Initializes the Bag
// Postcondition: Bag is initialized
Bag(const std::size_t initialCapacity = 20);
// Copy Constructor
// Postcondition: Bag is a copy of source with it's own dynamic memory
Bag(const Bag &source);
// Destructor for Bag. Frees up dynamic memory
~Bag();
// inserts an element into the Bag
// Precondition: size() < CAPACITY
// Postcondition: A new copy of entry has been added to the Bag
void insert(const value_type &x);
// attempts to remove one element that is equal to x from the Bag.
// Returns: true if the element was removed, else false.
// Precondition: none
// Postcondition: one element that is equal to x is removed from the Bag.
bool erase_one(const value_type &x);
// erases every element that is equal to x from the Bag.
// Returns: the number of elements erased
// Precondition: none
// Postcondition: all the elements that are equal to x are removed from the Bag
std::size_t erase(const value_type &x);
// returns the number of elements in the bag
// Precondition: none
// Postcondition: none
std::size_t size() const;
// returns the number of elements equal to x in the Bag
// Precondition: none
// Postcondition: none
std::size_t count(const value_type &x) const;
// returns the current maximum capacity of the Bag
std::size_t capacity () const;
// print the contents of the Bag
void print();
// overloaded assignment operator deallocates m_Data and allocates new memory for m_Data
// and copies the elements from source to m_Data
void operator=(const Bag &source);
private:
// if newCapacity > m_CurrentCapacity, allocates an array with size newCapacity
// on the Heap, copies all elements from m_Data to the new array, deallocates the
// old array memory and assigns m_Data to the new array.
void resizeArray(const std::size_t newCapacity);
// the number of elements currently in the Bag
std::size_t m_NumberOfElements;
// The max capacity of the Bag before it must be resized to accept more elements
std::size_t m_CurrentCapacity;
// where the Bag stores it's elements
value_type *m_Data;
};
}
#endif //PA5_BAG_H
////Bag.cpp
#include "Bag.h"
#include <algorithm>
#include <iostream>
CSCI2421::Bag::Bag(const std::size_t initialCapacity) : m_NumberOfElements(0), m_CurrentCapacity(initialCapacity)
{
m_Data = new value_type[initialCapacity];
}
CSCI2421::Bag::Bag(const CSCI2421::Bag &source) : m_NumberOfElements(source.m_NumberOfElements),
m_CurrentCapacity(source.m_CurrentCapacity)
{
m_Data = new value_type[m_CurrentCapacity];
std::copy (source.m_Data, source.m_Data + m_NumberOfElements, m_Data);
}
CSCI2421::Bag::~Bag()
{
delete[] m_Data;
}
void CSCI2421::Bag::insert(const value_type& x)
{
if (m_NumberOfElements == m_CurrentCapacity)
{
resizeArray(m_NumberOfElements + 1);
}
m_Data[m_NumberOfElements] = x;
m_NumberOfElements++;
}
bool CSCI2421::Bag::erase_one(const value_type& x)
{
for (std::size_t index = 0; index < m_NumberOfElements; index++)
{
if (m_Data[index] == x)
{
m_NumberOfElements--;
m_Data[index] = m_Data[m_NumberOfElements];
return true;
}
}
return false;
}
std::size_t CSCI2421::Bag::erase(const value_type& x)
{
std::size_t index = 0;
std::size_t numberRemoved = 0;
while (index < m_NumberOfElements)
{
if (m_Data[index] == x)
{
m_NumberOfElements--;
m_Data[index] = m_Data[m_NumberOfElements];
numberRemoved++;
}
else
{
index++;
}
}
return numberRemoved;
}
std::size_t CSCI2421::Bag::size() const
{
return m_NumberOfElements;
}
std::size_t CSCI2421::Bag::count(const value_type& x) const
{
std::size_t counter = 0;
for (std::size_t i = 0; i < m_NumberOfElements; i++)
{
if (m_Data[i] == x)
counter++;
}
return counter;
}
void CSCI2421::Bag::resizeArray(const std::size_t newCapacity)
{
if (newCapacity <= m_CurrentCapacity)
{
// already large enough
return;
}
value_type* largerArray = new value_type[newCapacity];
std::copy (m_Data, m_Data + m_CurrentCapacity, largerArray);
delete[] m_Data;
m_Data = largerArray;
m_CurrentCapacity = newCapacity;
}
void CSCI2421::Bag::operator=(const CSCI2421::Bag &source)
{
if (this == &source)
return;
value_type* newData;
if (m_CurrentCapacity != source.m_CurrentCapacity)
{
newData = new value_type[source.m_CurrentCapacity];
delete[] m_Data;
m_Data = newData;
m_CurrentCapacity = source.m_CurrentCapacity;
}
m_NumberOfElements = source.m_NumberOfElements;
std::copy (source.m_Data, source.m_Data + m_NumberOfElements, m_Data);
}
void CSCI2421::Bag::print()
{
std::cout << "Bag contains " << m_NumberOfElements << " elements: ";
for (std::size_t i = 0; i < m_NumberOfElements; i++)
{
std::cout << m_Data[i] << " ";
}
std::cout << std::endl;
}
size_t CSCI2421::Bag::capacity() const
{
return m_CurrentCapacity;
Explanation / Answer
I have converted the class definition into a template.
C++ code below:
#include<iostream>
using namespace std;
#include <cstdlib>
template< typename TYPE > class Bag
{
public:
typedef TYP value_type;
// Initializes the Bag
// Postcondition: Bag is initialized
Bag(const std::size_t initialCapacity = 20);
// Copy Constructor
// Postcondition: Bag is a copy of source with it's own dynamic memory
Bag(const Bag &source);
// Destructor for Bag. Frees up dynamic memory
~Bag();
// inserts an element into the Bag
// Precondition: size() < CAPACITY
// Postcondition: A new copy of entry has been added to the Bag
void insert(const value_type &x);
// attempts to remove one element that is equal to x from the Bag.
// Returns: true if the element was removed, else false.
// Precondition: none
// Postcondition: one element that is equal to x is removed from the Bag.
bool erase_one(const value_type &x);
// erases every element that is equal to x from the Bag.
// Returns: the number of elements erased
// Precondition: none
// Postcondition: all the elements that are equal to x are removed from the Bag
std::size_t erase(const value_type &x);
// returns the number of elements in the bag
// Precondition: none
// Postcondition: none
std::size_t size() const;
// returns the number of elements equal to x in the Bag
// Precondition: none
// Postcondition: none
std::size_t count(const value_type &x) const;
// returns the current maximum capacity of the Bag
std::size_t capacity () const;
// print the contents of the Bag
void print();
// overloaded assignment operator deallocates m_Data and allocates new memory for m_Data
// and copies the elements from source to m_Data
void operator=(const Bag &source);
private:
// if newCapacity > m_CurrentCapacity, allocates an array with size newCapacity
// on the Heap, copies all elements from m_Data to the new array, deallocates the
// old array memory and assigns m_Data to the new array.
void resizeArray(const std::size_t newCapacity);
// the number of elements currently in the Bag
std::size_t m_NumberOfElements;
// The max capacity of the Bag before it must be resized to accept more elements
std::size_t m_CurrentCapacity;
// where the Bag stores it's elements
value_type *m_Data;
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.