I
Immortal Nephi
I want to design three different classes. Three classes’ names are
Array_1D, Array_2D and Array_3D. Array_1D has all elements in
column. Array_2D enchances Array_1D by adding row like matrix. Also,
Array_3D enchanges Array_2D by adding plane like cube.
I wonder if I don’t like to define vector like below.
vector< int > Array_1D;
vector< vector< int > > Array_2D;
vector< vector< vector< int > > > Array_3D;
It is so confusing to me. I prefer to use only one vector. I can
add data members to that class like column, row, plane.
template< typename element_type >
class Array_1D {
public:
typedef typename vector< element_type >::size_type size_type;
Array_1D() {}
Array_1D( size_type column ) : m_column( column ) {
m_data.resize( column );
}
private:
vector< element_type > m_data;
size_type m_column;
};
template< typename element_type >
class Array_2D : public Array_1D< element_type > {
public:
typedef typename vector< element_type >::size_type size_type;
Array_2D() {}
Array_2D(
size_type column,
size_type row
) : m_column( column ), m_row( row ) {
m_data.resize( row * column );
}
private:
size_type m_row;
};
template< typename element_type >
class Array_3D : public Array_2D< element_type > {
public:
typedef typename vector< element_type >::size_type size_type;
Array_3D() {}
Array_3D(
size_type column,
size_type row
size_type plane
) : m_column( column ), m_row( row ), m_plane( plane ) {
m_data.resize( plane * row * column );
}
private:
size_type m_plane;
};
Do you see that three classes are clean readable code? If I want to
add some vector’s functions into my own class, I would write my own
function like begin(), end(), clear, empty, resize. They behave
differently because they are not the same as vector’s functions.
Also, I add Insert_Column, Insert_Row, Insert_Plane, Remove_Column,
Remove_Row, Remove_Plane functions.
I don’t use inheritance to override vector’s function. I use
composition and write my own functions.
My question is – is it ok if I write my own functions which they
behave like vector’s functionality? Also, I can write my own copy
constructor and assignment operator.
Array_1D, Array_2D and Array_3D. Array_1D has all elements in
column. Array_2D enchances Array_1D by adding row like matrix. Also,
Array_3D enchanges Array_2D by adding plane like cube.
I wonder if I don’t like to define vector like below.
vector< int > Array_1D;
vector< vector< int > > Array_2D;
vector< vector< vector< int > > > Array_3D;
It is so confusing to me. I prefer to use only one vector. I can
add data members to that class like column, row, plane.
template< typename element_type >
class Array_1D {
public:
typedef typename vector< element_type >::size_type size_type;
Array_1D() {}
Array_1D( size_type column ) : m_column( column ) {
m_data.resize( column );
}
private:
vector< element_type > m_data;
size_type m_column;
};
template< typename element_type >
class Array_2D : public Array_1D< element_type > {
public:
typedef typename vector< element_type >::size_type size_type;
Array_2D() {}
Array_2D(
size_type column,
size_type row
) : m_column( column ), m_row( row ) {
m_data.resize( row * column );
}
private:
size_type m_row;
};
template< typename element_type >
class Array_3D : public Array_2D< element_type > {
public:
typedef typename vector< element_type >::size_type size_type;
Array_3D() {}
Array_3D(
size_type column,
size_type row
size_type plane
) : m_column( column ), m_row( row ), m_plane( plane ) {
m_data.resize( plane * row * column );
}
private:
size_type m_plane;
};
Do you see that three classes are clean readable code? If I want to
add some vector’s functions into my own class, I would write my own
function like begin(), end(), clear, empty, resize. They behave
differently because they are not the same as vector’s functions.
Also, I add Insert_Column, Insert_Row, Insert_Plane, Remove_Column,
Remove_Row, Remove_Plane functions.
I don’t use inheritance to override vector’s function. I use
composition and write my own functions.
My question is – is it ok if I write my own functions which they
behave like vector’s functionality? Also, I can write my own copy
constructor and assignment operator.