Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Please write the following C++ program and show all the outputs. Copy the Que

ID: 3847826 • Letter: 1

Question

1. Please write the following C++ program and show all the outputs.

Copy the Queue class from the earlier (see earler assigment below labled earlear assignment) into a new directory to use it as a starting point for this assignment.

1. Change your Queue class to take an STL container as a parameter, and use this container inside the Queue class for storage, instead of using your own like you may have been earlier. Even if you used an STL container for storage earlier, you now need to take the STL container as a parameter to the class. This means you can instantiate your Queue class to use a deque, or list.

This will also mean that you now don’t need to keep track of size / capacity, etc since the STL container will do that for you. So feel free to not use the base class in this assignment, i.e., your Queue class will not derive from any other class.

Note that you have access to the element type inside an STL container by accessing value_type. For example, if you have passed a deque<int> to your Queue class, how do you tell inside the Queue class what the return type of Pop should be. It would be int if you passed in deque <int>, it would be string if you passed in deque <string>. So you could do a typedef of the value_type to know what the element type of the container is:

typedef typename Container::value_type Element; // put this inside the Queue class

Now, inside your Queue class, you can say:

Element Pop(); void Push( const Element & e );

And your template class will look something like:

template <typename Container> class MyQueue { … private: Container mCollection; };

And you would instantiate it as below:

MyQueue< deque<double> > qi; MyQueue< deque<string> > qs; MyQueue< list<int> > qli;
Assignment 5 (Template specialization)

2. Specialize this Queue class that you create in 1 above for a deque<int>.

This means that if you do the following:

MyQueue< deque<int> > qi;

It will use your specialized class. Note that you don’t have to make the interface of your specialized class the same as your non-specialized template class. If you want to make it different, that is fine. Since your Queue class takes only 1 parameter, what you do in 2 above is total specialization.

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

Eailier assignment your using:

#include<iostream>
#include <vector>
#include <fstream>
using namespace std;
//Defines a template class
template <typename T>
class MyCollection
{
   //Public member function
public:
   //To add data to vector
   void Add(const T &);
   //To return number of items in the vector
   int Count() const;
   //To check whether the vector is empty or not
   bool IsEmpty() const;

   // To return the value of the vector at index position
   T Get(int index) const;
   //To print the contents of vector
   void PrintAll(std::ostream & os);
   //To print the contents of the vector reverse order
   void PrintAllReverseOrder(ostream & os);

private:
   std::vector<T> mCollection;
};//End of class

//To print the vector contents in reverse order
template <typename T>
void MyCollection<T>::PrintAllReverseOrder(ostream & os)
{
   //Begins from size minus one position to the zero position
   for (int i = mCollection.size() - 1; i >= 0; i--)
       //Display the contents of vector at i position
       os << ' ' << mCollection[i];
   os << ' ';
}//End of function

//To return the value of the vector at index position
template <typename T>
T MyCollection<T>::Get(int index) const
{
   //Checks if the index is greater than zero and less than size then valid index position
   if (index > 0 && index < mCollection.size())
       //Return the value at index position
       return mCollection[index];
   //Otherwise return zero
   else
   {
       cout << " Invalid index";
       return 0;
   }
}//End of function

//Returns true if the vector is empty otherwise false
template <typename T>
bool MyCollection<T>::IsEmpty() const
{
   //Checks whether the vector is empty or not
   if (mCollection.empty())
       return true;
   else
       return false;
}//End of function

//Returns number of items available in the vector
template <typename T>
int MyCollection<T>::Count() const
{
   return mCollection.size();
}//End of function

//Adds an item to the vector
template <typename T>
void MyCollection<T>::Add(const T &data)
{
   mCollection.push_back(data);
}//End of function

//Print the contents of the vector
template <typename T>
void MyCollection<T>::PrintAll(ostream & os)
{
   //Loops from zero to size
   for (int i = 0; i < mCollection.size(); i++)
       os << ' ' << mCollection[i];
   os << ' ';
}//End of function

int main()
{
   int pos;
   //For string
   MyCollection<string> str;
   cout << " Using string: ";
   //Add data
   str.Add("This");
   str.Add("is");
   str.Add("demo");
   //Print data
   str.PrintAll(std::cout);
   cout << " Count = " << str.Count();
   cout << " Empty Status: ";
   if (str.IsEmpty())
       cout << "Empty";
   else
       cout << "Not Empty";

   cout << " Enter the index position: ";
   cin >> pos;
   cout << " Value at " << pos << " index position: " << str.Get(pos);

   cout << " Reverse Order: ";
   str.PrintAllReverseOrder(std::cout);

   //For integer
   MyCollection<int> integer;
   cout << " Using integer: ";
   integer.Add(100);
   integer.Add(50);
   integer.Add(10);
   integer.Add(5);
   integer.PrintAll(std::cout);
   cout << " Count = " << integer.Count();
   cout << " Empty Status: ";
   if (integer.IsEmpty())
       cout << "Empty";
   else
       cout << "Not Empty";

   cout << " Enter the index position: ";
   cin >> pos;
   cout << " Value at " << pos << " index position: " << integer.Get(pos);

   cout << " Reverse Order: ";
   integer.PrintAllReverseOrder(std::cout);

   return 0;
}//End of function

Explanation / Answer

#include<iostream>
#include <vector>
#include <fstream>
using namespace std;
//Defines a template class
template <typename T>
class MyCollection
{
   //Public member function
public:
   //To add data to vector
   void Add(const T &);
   //To return number of items in the vector
   int Count() const;
   //To check whether the vector is empty or not
   bool IsEmpty() const;

   // To return the value of the vector at index position
   T Get(int index) const;
   //To print the contents of vector
   void PrintAll(std::ostream & os);
   //To print the contents of the vector reverse order
   void PrintAllReverseOrder(ostream & os);

private:
   std::vector<T> mCollection;
};//End of class

//To print the vector contents in reverse order
template <typename T>
void MyCollection<T>::PrintAllReverseOrder(ostream & os)
{
   //Begins from size minus one position to the zero position
   for (int i = mCollection.size() - 1; i >= 0; i--)
       //Display the contents of vector at i position
       os << ' ' << mCollection[i];
   os << ' ';
}//End of function

//To return the value of the vector at index position
template <typename T>
T MyCollection<T>::Get(int index) const
{
   //Checks if the index is greater than zero and less than size then valid index position
   if (index > 0 && index < mCollection.size())
       //Return the value at index position
       return mCollection[index];
   //Otherwise return zero
   else
   {
       cout << " Invalid index";
       return 0;
   }
}//End of function

//Returns true if the vector is empty otherwise false
template <typename T>
bool MyCollection<T>::IsEmpty() const
{
   //Checks whether the vector is empty or not
   if (mCollection.empty())
       return true;
   else
       return false;
}//End of function

//Returns number of items available in the vector
template <typename T>
int MyCollection<T>::Count() const
{
   return mCollection.size();
}//End of function

//Adds an item to the vector
template <typename T>
void MyCollection<T>::Add(const T &data)
{
   mCollection.push_back(data);
}//End of function

//Print the contents of the vector
template <typename T>
void MyCollection<T>::PrintAll(ostream & os)
{
   //Loops from zero to size
   for (int i = 0; i < mCollection.size(); i++)
       os << ' ' << mCollection[i];
   os << ' ';
}//End of function

int main()
{
   int pos;
   //For string
   MyCollection<string> str;
   cout << " Using string: ";
   //Add data
   str.Add("This");
   str.Add("is");
   str.Add("demo");
   //Print data
   str.PrintAll(std::cout);
   cout << " Count = " << str.Count();
   cout << " Empty Status: ";
   if (str.IsEmpty())
       cout << "Empty";
   else
       cout << "Not Empty";

   cout << " Enter the index position: ";
   cin >> pos;
   cout << " Value at " << pos << " index position: " << str.Get(pos);

   cout << " Reverse Order: ";
   str.PrintAllReverseOrder(std::cout);

   //For integer
   MyCollection<int> integer;
   cout << " Using integer: ";
   integer.Add(100);
   integer.Add(50);
   integer.Add(10);
   integer.Add(5);
   integer.PrintAll(std::cout);
   cout << " Count = " << integer.Count();
   cout << " Empty Status: ";
   if (integer.IsEmpty())
       cout << "Empty";
   else
       cout << "Not Empty";

   cout << " Enter the index position: ";
   cin >> pos;
   cout << " Value at " << pos << " index position: " << integer.Get(pos);

   cout << " Reverse Order: ";
   integer.PrintAllReverseOrder(std::cout);

   return 0;
}//End of function