Use classes as arrays in c++ -
what want do: want know how use classes kind of 'built-in' arrays in c++(c acceptable). so, want able this:
array1d rows(5); //an integer array of length 5 - iniatilized 0 upon instantiation. //rows can imagined this: [ 0, 0, 0, 0, 0 ] array1d x(1), y(2), z(3); array2d cols(3); cols[0] = x; cols[1] = y; cols[2] = z; //an array of array of integers [ [x] [y] [z] ] //where x, y , z of type array1d, here like: //if x = 1, y = 2, z = 3 //[ [0], [0, 0], [0, 0, 0] ] array3d depth(2); //an array of array of array of integers [ [cols] [cols] ] //where cols of type array2d, when expanded like: //[ [ [0], [0, 0], [0, 0, 0] ] // [ [0], [0, 0], [0, 0, 0] ] ]
what have far
using namespace std; class array3d; class array2d; class array1d//works way want { int len; int *data; public: array1d (int size){ data = new (nothrow) int[size]; if (data != 0){ len = size; (int = 0; i<len; i++){ data[i] = -1; } } } ~array1d (){ delete data; } void print(){ (int = 0; i<len; i++){ cout << data[i] << " "; } cout << endl; } int operator[] (int index){ if (index >= 0 && index <= len){ return data[index]; } } friend class array2d; }; class array2d//this needs changing { int len; array1d *data; public: array2d (int size_dim1, int size_dim2){ data = new (nothrow) array1d[size_dim1]; //i think problem here if (data !=0){ len = size_dim1; (int = 0; < len; i++){ data[i] = array1d(size_dim2); } } } ~array2d (){} void print(){ (int = 0; < len; i++){ data[i].print(); } } array1d operator[] (int index){ return data[index]; } friend class array3d; }; class array3d//dont know how 1 started { array3d (int size_dim1, int size_dim2, int size_dim3){ data = new (nothrow) array2d[size_dim2, size_dim3]; if (data !=0){ len = size_dim1; (int = 0; < len; i++){ data[i] = array2d(size_dim2, size_dim3); } } } ~array3d (){} //overload [] operator private: int len; array2d *data; };
i want able add more classes accomodate possible 4 dimension such dont have customize each , every time add class.
you can't put different type of items array. in case, since 2d class contains 1d class, 3d class contains 2d class, can this:
class array1d { // }; class array2d : public array1d { // }; class array3d : public array2d { // };
and put pointers array.
array1d *arr[3]; arr[0] = new array1d; arr[1] = new array2d; arr[3] = new array3d;
Comments
Post a Comment