Questions about creating a 3D array class
-
I am doing one project in the book "Data Structures and Algorithms with Object-Oriented Design Patterns in C++". The project is designing a 3D array class based on previous 2D array class. Code as follows: template class Array2D { public: Array2D(int w, int h):width(w), height(h), array(w*h) {} ... ... protected: int width, height; Array array; } template class Array3D { public: Array3D(int l, int w, int h):length(l), width(w), height(h), array(l) {} ... ... protected: int length, width, height; Array< Array2D > array; } I am planning to create a array with elements of 2D array. I could use the initializer "array(l)" to define the length of array. But the size of element "Array2D" should be "width*height". But I don't know how to initialize the size of "Array2D" in the class of Array3D. Could anybody give me some advice please?
-
I am doing one project in the book "Data Structures and Algorithms with Object-Oriented Design Patterns in C++". The project is designing a 3D array class based on previous 2D array class. Code as follows: template class Array2D { public: Array2D(int w, int h):width(w), height(h), array(w*h) {} ... ... protected: int width, height; Array array; } template class Array3D { public: Array3D(int l, int w, int h):length(l), width(w), height(h), array(l) {} ... ... protected: int length, width, height; Array< Array2D > array; } I am planning to create a array with elements of 2D array. I could use the initializer "array(l)" to define the length of array. But the size of element "Array2D" should be "width*height". But I don't know how to initialize the size of "Array2D" in the class of Array3D. Could anybody give me some advice please?
One solution can help you to make something new: class Array2D { public: Array2D(int w, int h):width(w), height(h), array(w*h) {} ... ... protected: int width, height; Array array; } template class Array3D :public Array2D { public: Array3D(int l, int w, int h) : Array2D( w, h), length(l) {} ... ... protected: int length; //Array< Array2D > array; }