Thursday 15 July 2010

Making an Array that stores pairs and or a class in c++? -



Making an Array that stores pairs and or a class in c++? -

i've looked while , none of answers seem fit question, or i've nail mental barrier.

i've made class has set , methods , output method called pairs, works 2 ints x+y , each have set , methods individually. need create dynamic array based on code below can store these pairs. i'm not looking spoon fed want know if logic right , pointed in right direction.

class dynarray //a dynamic(ish) array class { public: dynarray() : data(0), sz(0) { } dynarray(int size) : sz(size) { info = new int[sz]; } virtual ~dynarray() { if(data) delete [] data; } //destructor method int& operator [] (int idx) { homecoming data[idx]; } //operator overload! int size() { homecoming sz; } private: int * data; int sz; };

pairs class

class pairs{ public: pairs() : x(0), y(0) { } void setx(int); void sety(int); void outputxy(int, int); int getx(); int gety(); private: int *x; int *y; }; void pairs::setx(int a) { x = &a; } void pairs::sety(int b) { y = &b; } int pairs::getx() { homecoming *x; } int pairs::gety() { homecoming *y; } void pairs::outputxy(int c, int d) { x = &c; y = &d; cout << "number 1:" << c << " number 2:" << d; }

i'm rusty c++ logic has vanished, i'm struggling see how create array can store 2 ints within 1 element. thought maybe create pair class array stores 2 numbers in element 0 , 1 , create array of class using dynamic code, figuring out how has stumped me though. need helpful link or terms thrown @ me want larn code myself. help appreciated, thanks.

i recommend using stl library, in particular std::pair , kind of container std::lists or std::vectors. in order implement own, additional functionality, may create own class derived those.

(not plenty "reputation" post reply comment...)

c++ arrays class inheritance overloading

No comments:

Post a Comment