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

Write a template class named S hiftVec. Its only data member will be a vector wh

ID: 3763708 • Letter: W

Question

Write a template class named ShiftVec. Its only data member will be a vector whose type will match the type of the ShiftVec object. For instance a ShiftVec would contain a vector. It should have a constructor that takes as a parameter a vector (of the appropriate type) and assigns it to its data member. It should have a get method called getVec() that returns the value of the vector data member. It should have a void method called shiftRight() that takes an int parameter and shifts all the elements of the vector that many positions to the right, wrapping back around to the beginning where necessary. You can assume that the parameter is a non-negative integer - it is allowed to be larger than the size of the vector, in which case some or all elements may wrap around multiple times. It should have a void method called shiftLeft() that works the same way, but in the opposite direction.

For example, if you have a vector named myVec that contains [23, 2, 17, 12, 4], and you do the following:

Then the result vector should contain [12, 4, 23, 2, 17].

The files must be called ShiftVec.hpp and ShiftVec.cpp.

At the end of the ShiftVec.cpp file, you must put the following lines:

The reason is that since the template definition and the main method that uses it are in separate "compilation units", the compiler doesn't see what types of ShiftVec classes need to be created, so it doesn't create them. Putting in those four lines forces the compiler to create those four versions of the ShiftVec class. This way of handling it can only be used if you know ahead of time which types of the template classes will be needed. The other way to handle this is to put all of the template definition into the header file, which is the way the Standard Template Library handles it, but is not what you will do for this project.

PS Interested in seeing a solution with STL algorithm swap

Explanation / Answer

ShiftVec.hpp:

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

#ifndef SHIFTVEC_HPP_
#define SHIFTVEC_HPP_

using namespace std;

#include <vector>

//template class ShiftVec

template <class T> class ShiftVec {
private:
   vector<T> data; //vector data member
public:
   ShiftVec(vector<T>); //constructor
   vector<T> getVec(); //returns vector data
   void shiftRight(int); //shifts elements to right as per specified parameter
   void shiftLeft(int); //shifts elements to left as per specified parameter
};

#endif /* SHIFTVEC_HPP_ */

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

shiftvec.cpp:

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

//Implementation details of ShiftVec class

#include <iostream>
#include "ShiftVec.hpp"
using namespace std;

//implementation of constructor
template <class T> ShiftVec<T>::ShiftVec(vector<T> vec)
{
   this->data=vec;
}

//implementation of getVec()
template <class T> vector<T> ShiftVec<T>::getVec()
{
   return data;
}

//implementation of shiftRight()
template <class T> void ShiftVec<T>::shiftRight(int pos)
{
   vector<T> vec=data;
   int size=vec.size(); //size of vector
   vector<T> newvec(size);
   cout<<"vector size:"<<size<<endl;
   int index; //index at which element is to be stored
   int count;
   for(int i=0;i<size;i++)
   {
       T element=vec[i]; //element to be shifted
       index=i;
       count=1;
       while(count<=pos)
       {
           if(index==(size-1))
               index=0;
           else
               index++;
           //cout<<"index values:"<<index<<endl;
           count++;
       }
       //cout<<"count:"<<count<<endl;
       //cout<<"new index:"<<index<<endl;
       newvec[index]=element;
   }
   data=newvec;
}

//implementation of shiftLeft()
template <class T> void ShiftVec<T>::shiftLeft(int pos)
{
   vector<T> vec=data;
   vector<T> newvec;
   int size=vec.size(); //size of vector
   int index; //index at which element is to be stored
   int count;
   for(int i=0;i<size;i++)
   {
       T element=vec[i]; //element to be shifted
       index=i;
       count=1;
       while(count<=pos)
       {
           if(index==0)
               index=size-1;
           else
               index--;
           count++;
       }
       newvec[index]=element;
   }
   data=newvec;
}

template class ShiftVec<int>;
template class ShiftVec<double>;
template class ShiftVec<bool>;
template class ShiftVec<std::string>;

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

main.cpp():

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

using namespace std;

#include <iostream>
#include <vector>
#include "ShiftVec.hpp"

int main() {
   int elements[]={23,2,17,12,4};
   vector<int> myVec(elements,elements+sizeof(elements)/sizeof(int));
   for(unsigned int i=0;i<myVec.size();i++)
   {
       cout<<myVec[i]<<" ";
   }
   cout<<endl;
   ShiftVec<int> sv(myVec);
   sv.shiftRight(12);
   vector<int> result=sv.getVec();
   for(unsigned int i=0;i<result.size();i++)
   {
       cout<<result[i]<<" ";
   }
   return 0;
}

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote