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

Q1 Class Vec int Q1.i Previous class declaration e Recall the declaration of the

ID: 3916828 • Letter: Q

Question

Q1 Class Vec int Q1.i Previous class declaration e Recall the declaration of the class Vecint . We shall overload some operators for the class Vec.int. class Vec int ( public Vec int (O; Vec int (int n); Vec int (int n, int a) Vec int (const Veclint &orig;): Vec intk operator- (const Vec int &rhs;); Vec int O; int capacity) const; int size) const; int front ) const; int back() const; void clear; void pop.back); void push_back (int a); int& at (int n); private: void allocate) void release ) int _capacity; int size; int * vec;

Explanation / Answer

modified c++ source code:-
====================
#include <iostream>
#include <vector>
#include<stdlib.h>
using namespace std;
template<typename T>
class Mvector
{
public:
Mvector();
vector<int> vec;
std::vector<int>::iterator it;
Mvector(unsigned int n);
void pushback(T x);
void popback();
void clear();
void Display();
void insert(int i, T x);
void erase(int i);
T operator[] (unsigned int i);
int size();
private:
int vsize;
int vcap;
T *v;
void reserve(unsigned int n);
};
//popback Fun ction Definition
template <class T>
void Mvector<T> ::popback()
{
vec.pop_back();
cout<<" Last Element Deleted Successfully:"<<endl;
}
//Size Function Definition
template <class T>
int Mvector<T> ::size()
{
return vec.size();
}

//Display Function Definition
template <class T>
void Mvector<T> ::Display()
{
cout<<" The Vector Elements are:"<<endl;
for(it=vec.begin(); it<vec.end(); it++)
std::cout << ' ' << *it;
std::cout << ' ';
}
//Pushback Function Definistion
template <class T>
void Mvector<T> ::pushback(T x)
{
vec.push_back(x);
}
//Erase Function Definistion
template <class T>
void Mvector<T> ::erase(int element)
{
vec.erase (vec.begin()+element);
}
//Insert Function Definition
template <class T>
void Mvector<T> :: insert(int i,T value)
{
vec.insert (it,i,value);
v[i]=value;
cout<<" Inserted Element Successfully"<<endl;
}
template <class T>
Mvector<T> :: Mvector()
{
cout<<" Constructor is called"<<endl;
}
template <class T>
Mvector<T> :: Mvector(unsigned int m)
{
vsize=m;
v = new T[vsize];
for(int i=0;i<vsize;i++)
{
v[i]=0;
}
}
int main()
{
int size;
int value,option,position;
cout<<" ---------------------------------"<<endl;
cout<<" Implementation of Vector Using Template"<<endl;
cout<<" Please Read the Size of Array:"<<endl;
cin>>size;
int *integer_array=new int[size];
Mvector <int> obj(size);
cout<<" Please Enter "<<size<<" Elements:"<<endl;
for(int i=0;i<size;i++)
{
cin>>value;
integer_array[i]=value;
}
while(1)
{
cout<<" --------------------"<<endl;
cout<<" ****** MENU ******"<<endl;
cout<<" 1.Insert data particular Postion"<<endl;
cout<<" 2.pushback"<<endl;
cout<<" 3.Erase The Elements:"<<endl;
cout<<" 4.popback"<<endl;
cout<<" 5.Display"<<endl;
cout<<" 6.Clear"<<endl;
cout<<" 7.Size"<<endl;
cout<<" 9.Exit"<<endl;
cout<<" Select any Option:"<<endl;
cin>>option;
switch(option)
{
case 1:
cout<<" Please Enter the position:"<<endl;
cin>>position;
if(position>size)
{
cout<<" Insertion is Not Possible"<<endl;
}
else
{
cout<<" Please Enter the Value:"<<endl;
cin>>value;
obj.insert(position,value);
}
break;
case 2:
cout<<" Please Enter the New Value: "<<endl;
cin>>value;
obj.pushback(value);
break;
case 3:
cout<<" Please Enter How Many ELements want to Erase:"<<endl;
cin>>value;
obj.erase(value);
break;
case 4:
obj.popback();
break;
case 5:
obj.Display();
break;
case 7:
cout<<" The Size of Vector is:" <<obj.size();
break;
case 9:
exit(0);
default:
cout<<" Wrong Choice Please try Again"<<endl;
}
}
}