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

1. Please write a c++ program and show all outputs. Implement a MyCollection cla

ID: 3842013 • Letter: 1

Question

1. Please write a c++ program and show all outputs.

Implement a MyCollection class that takes the type that is stored in the collection as a template parameter (like you specify the type you want to store in a vector ). Implement the methods specified below. The PrintAll and PrintAllReverseOrder should be implemented using STL iterators.

Note: 1. The syntax I have below is not correct / complete for specifying the template class… you need to complete it. 2. The element type you want to store in this collection is up to you, but you should have one example of your own class type (any class you like, you can use the Student /Circle class from earlier assignments, or make up some simple class of your own. What operator do you need to override for this class of yours such that the following template class would work ?

class MyCollection { public:     MyCollection;     void Add( const T & );   int Count() const;   bool IsEmpty() const;

// The return value of Get should be a const reference to the
// stored type
   Get(int index ) const;       void PrintAll( ostream & os );   void PrintAllReverseOrder( ostream & os );
Template assignment, C++ 712

   private:     std::vector<T> mCollection; };
Now, you should be able to execute the following:

int main(int argc, char ** argv ) {
// Instantiate MyCollection of integers. Lets say its called m1. // Add some elements to it by calling m1.Add      m1.PrintAll( std::cout );   std::cout << endl;   m1.PrintAllReverseOrder( std::cout );     return 0; }

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

Output:

Using string: This is demo

Count = 3
Empty Status: Not Empty
Enter the index position: 1

Value at 1 index position: is
Reverse Order: demo is This


Using integer: 100 50 10 5

Count = 4
Empty Status: Not Empty
Enter the index position: 2

Value at 2 index position: 10
Reverse Order: 5 10 50 100