using c++ and can you please add note so i can tell which function is which In t
ID: 3877576 • Letter: U
Question
using c++ and can you please add note so i can tell which function is which
In this task you are required to impalement your own C++ vector class. Name your class as Custom_Vector. The Custom_Vector class has to include at least following member functions:
1. A default constructor that initialize your custom vector objects;
2. A copy constructor;
3. A destructor to release the memory to the heap;
4. push_back(string item) function that appends elements to the end of the vector;
5. pop() function that removes the last element of your vector and returns it.
6. display() function that displays the elements of the vector.
7. size() function that returns the number of elements in the vector (logical size of the vector).
8. at(int index) function that returns the element at the position index (assume that the first element in your vector at position 1.)
Your class accepts only string values. Therefore, you do not need to use templates. Implement a main function to test all of your methods.
Hint: you can assume that the initial physical size of the vector is 10. If your vector is full and you do not have any empty elements, then create a new vector with a size double the previous size (e.g., if the old size is 10 then the new one will be 20). After that, copy all elements from the old vector to the new one and make all other necessary changes. Finally, delete the old vector.
Explanation / Answer
using namespace std;
#include <iostream>
class CustomVector {
private:
std::string *v;
int vectorSize;
int numOfElements;
public:
CustomVector() { //Default Constructor
vectorSize = 10;
numOfElements = 0;
v = new string[vectorSize];
for(int i =0;i<vectorSize;i++) {
v[i] = "";
}
}
CustomVector(const CustomVector &original) { //Copy Constructor
vectorSize = original.vectorSize;
for(int i =0;i<vectorSize;i++) {
v[i] = original.v[i];
}
}
void push_back(string item) { //pushes the new element to the last of the vector. If vector is full the resizes the vector to double size.
if(numOfElements<vectorSize) {
numOfElements++;
v[numOfElements-1] = item;
} else {
string *newVector = new string[vectorSize*2];
int i;
for(i =0;i<numOfElements;i++) {
newVector[i] = v[i];
}
newVector[i] = item;
delete[] v;
v= newVector;
numOfElements++;
vectorSize*=2;
}
}
string pop() { //removes the last element of the vvector.
string item = v[numOfElements-1];
numOfElements--;
return item;
}
int size() { // returns the number of elements in the vector.
return numOfElements;
}
string at(int index) { //function that returns the element at a specific index. If index is wrong it returns Invalid Index.
if(index>=1&&index<=numOfElements)
return v[index-1];
else
return "Invalid Index";
}
~CustomVector() { //Destructor
delete[] v;
}
void display() { //display function
for(int i =0;i<numOfElements;i++) {
std::cout<<v[i]<<" ";
}
}
};
int main(int argc, char *argv[]) {
CustomVector cv;
cv.push_back("String1");
cv.push_back("String2");
cv.push_back("String3");
cv.push_back("String4");
cv.push_back("String5");
cv.push_back("String6");
cv.push_back("String7");
cv.push_back("String8");
cv.push_back("String9");
cv.push_back("String10");
cv.push_back("String11");
cv.push_back("String12");
cv.display();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.