Hello, our tutor set us a task to do , which was make a vector out of a class. So I've implemented a basic one but it doesnt work well... its runs ect ... but when you re print the entered elements it doesnt print the correct ones:
#include <iostream>
using namespace std;
class vector
{
public:
vector(int ); //constructor
~vector(); //destructor
void insertAt( int value);
int pop();
int getmaxSize();
int gettop();
private:
int *theArray;
int maxsize;
int first;
};
vector::vector(int max)
{
theArray = new int[max];
maxsize = max;
first = -1;
}
vector::~vector()
{
delete[] theArray;
}
int vector::gettop()
{
return first;
}
int vector::getmaxSize()
{
return maxsize;
}
void vector::insertAt( int value)
{
if( first >=0)
{
maxsize++;
theArray[maxsize] = value;
}
}
int vector::pop()
{
if(maxsize >=0)
{
int temp = theArray[maxsize];
maxsize--;
return maxsize;
}
else
{
cout << "Stack empty" << endl;
return 0;
}
}
int main()
{
int sizeOfVector;
int number;
int i;
cout << "Hello and welcome to my class vectors...." <<endl;
cout << "Please enter the size of the vector" <<endl;
cin >>sizeOfVector;
vector v1(sizeOfVector);
cout << "You've created an vector with the array size of : " << v1.getmaxSize() <<endl;
cout << "The first element is: " << v1.gettop() <<endl;
for ( i = 0; i < sizeOfVector; i++)
{
cin >> number;
v1.insertAt(number);
}
cout <<"I'm now going to pop them off the stack" <<endl;
for (i = 0; i < sizeOfVector; i++)
cout << v1.pop()<<endl;
return 0;
}
heres what it does...say you entered the size 4: and enter 1,2,3,4. It prints out 3,2,1'0' ... but 0 wasn't entered....umm any ideas?