C++ Code Using the listType code from Chapter 13 of your text as a guide, create
ID: 3826731 • Letter: C
Question
C++ Code
Using the listType code from Chapter 13 of your text as a guide, create a template class bagType which may be filled with any one of the standard data types including, as a minimum, integers, floats, characters, and strings. Your class should include function templates to
· print the contents of a bag
· display the total capacity of a bag
· display the number of items currently in the bag
· determine if the bag is empty
· determine if the bag is full
Write a driver program to test your class and functions. Your driver program should include code to create a bag object that holds each of the types noted above and performs each of the functions for each type of bag.
Your main program should also implement exception handling using try and catch blocks as described in Chapter 14 of your text.
Do not use exception handling to ensure the user input matches the type requested. If you want to include that in your program use the logic that we have used previously to implement it (a while loop). For the purposes of this program, your bag should only accept positive values for the numeric types and only upper case values for the bag of characters. You do not need to write exception handling code for your bag of strings. Your code should include hard-coded input that tests the exception handling as well as code that generates correct input.
Explanation / Answer
//bagType.h
#include<iostream>
#include<string>
using namespace std;
template<typename T>
class bagType;
template<class T>
class bagType
{
T *items;
int capacity;
int no_items;
public:
bagType<T>()
{
items = NULL;
no_items = 0;
capacity = 0;
}
bagType<T>(int cap)
{
items = NULL;
no_items = 0;
if (cap < 0)
throw "capacity cannot be negetive";
capacity = cap;
}
bool isEmpty()
{
if (no_items == 0)
return true;
else
return false;
}
bool isFull()
{
if (no_items == capacity)
return true;
else
return false;
}
void print()
{
cout << "Total capacity of bag = " << capacity << endl;;
for (int i = 0; i < no_items; i++)
{
cout << items[i] << " ";
}
}
void add(T i)
{
if (isEmpty())
{
items = new T[capacity];
items[no_items++] = i;
}
else
{
if (isFull())
{
cout << "Bag is full , cannot add items" << endl;
}
items[no_items++] = i;
}
}
T operator[](int index);
~bagType()
{
delete[] items;
}
};
--------------------------------------------
//main.cpp
#include"bagType.h"
int main()
{
int cap = 5;
bagType<int> bagInt(cap);
bagType<char> bagChar(cap);
bagType<string> bagStr(cap);
bagType<float> bagFloat(cap);
//add items to bag
bagInt.add(4);
bagInt.add(3);
bagInt.add(10);
bagInt.add(5);
bagInt.print();
bagChar.add('A');
bagChar.add('C');
bagChar.add('D');
bagChar.add('E');
bagChar.print();
bagStr.add("This");
bagStr.add("is");
bagStr.add("Testing");
bagStr.print();
bagFloat.add(4.23);
bagFloat.add(3.89);
bagFloat.add(10.789);
bagFloat.add(5.56);
bagFloat.print();
}
---------------------------------------------------------
//output
Total capacity of bag = 5
4 3 10 5 Total capacity of bag = 5
A C D E Total capacity of bag = 5
This is Testing Total capacity of bag = 5
4.23 3.89 10.789 5.56
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.