thanks for the reply. here's what i have so far from the three classes of the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#ifndef ITIntVector_H
#define ITIntVector_H
class ITIntVector
{
public:
ITIntVector();
void push_back(int val);
int getPosition(int i);
int getSize();
int getCapacity();
void resize(int i);
private:
int theSize;
int theCapacity;
int *theArray;
};
#endif
| |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include "ITIntVector.h"
#include <iostream>
using namespace std;
ITIntVector::ITIntVector()
{
theSize = 10;
theCapacity = 0;
theArray = new int[theSize];
}
int ITIntVector::getSize()
{
return theSize;
}
int ITIntVector::getCapacity()
{
return theCapacity;
}
/*
void ITIntVector::push_back(int val)
{
if(theSize == theCapacity)
{
theArray[theSize + 1] = val;
theSize++;
}
//still working on this
}
*/
int ITIntVector::getPosition(int i)
{
int *p;
p = &i;
return theArray[*p];
}
| |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include "ITIntVector.h"
#include<iostream>
using namespace std;
int main()
{
ITIntVector v;
cout << "Capacity: " << v.getCapacity() << endl;
cout << "Size: " << v.getSize() << endl;
cout << v.getPosition(3) << endl;
system("pause");
return 0;
}
| |
this is the way we have to 'set up' the program, .h file, a ITIntVector.cpp file and a main.cpp. we have to delcare the dynamic array in the .h file and then in the constructor of the ITIntVector.cpp file make it's size 10 (or any other size). then use each function we write in the main.cpp on an instance of ITIntVector, im my case 'v'. is it because there's nothing in the array i'm getting that output as in my first post? here's the actual question, if it makes things any clearer for you:
1. Write your own vector class ITIntVector to use as a “smart array” with integers.
Methods of your class will be things like:
push_back(int val) to add an element to the end of the vector
at(int i) to return the element at index position i in your vector
size() to return the no of elements in the array
capacity() to return the amount of space currently allocated for the array
resize(int i) to change the size of the array – setting new elements to zero if
i is larger than the present size of the array, or chopping off the end of the array if
smaller