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

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 dont 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 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 , it would be string if you passed in deque . 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 class MyQueue { private: Container mCollection; }; And you would instantiate it as below: MyQueue< deque > qi; MyQueue< deque > qs; MyQueue< list > qli; Assignment 5 (Template specialization) 2. Specialize this Queue class that you create in 1 above for a deque. This means that if you do the following: MyQueue< deque > qi; It will use your specialized class. Note that you dont 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 #include #include using namespace std; //Defines a template class template 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 mCollection; };//End of class

//To print the vector contents in reverse order template void MyCollection::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 T MyCollection::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 bool MyCollection::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 int MyCollection::Count() const { return mCollection.size(); }//End of function

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

//Print the contents of the vector template void MyCollection::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 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 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!