Write a class that implements a vector of integers with the following template:

Class IntVector{ private:
// all member variables public:
IntVector( int n); // create a vector with size n
~IntVector(); // releases any memory allocated by the vector
Int getElement(int i); //returns the element i in the vector;
void setElement( int i, int v); // sets the element at location i to v
int getSize(); // returns the size of the vector;
void pushBack(int v); // adds the element v at the end of the array. The size of the array increases by 1.
bool popBack(); //removes the last element of the vector if any, and returns true, otherwise returns false }

- -
All methods, constructors and destructor prototypes described in the above class template have to be implemented
You can add any member variable you need to implement the class, but it has to be private.
You can add any extra method you might need to implement your class.

    #include <iostream>
    using namespace std;

    class IntVector
           vector<int> v;
           IntVector(int n)
                 v.size = n;
           int getElement(int i)
                 return v[i];
           void setElement(int i, int e)
                 std::vector<int>::iterator it = v.begin() + i;
                 v.insert(it, e);
           int getSize()
                 return v.size();
           void pushBack(int e)
           bool popBack()
                 int s = v.size();
                 int s2 = v.size();
                 if (s > s2)
                        return true;
                        return false;


I think there is something wrong but I don't know what could anyone help me!!

Line 12 makes no sense. vector::size() is how to ask a vector what size it is. It is not how to set the size of a vector.

Line 16 serves no purpose. Your internal vector object will be destructed without you having to do anything.

Your setElement function changes the size of the vector. It's not meant to.

If I had to guess, the purpose of this exercise is not for you to write a thin wrapper around std::vector, but the instructions don't forbid it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.