我在教育目的上做了這個。 這樣的想法: 我有類Array。我繼承它的類Darray,並添加必要的尺寸C++繼承陣列的darray
它喜歡這樣:
的main.cpp
int main(int argc, char *argv[])
{
Array obj;
obj.output();
DArray obj2;
obj2.output();
return 0;
}
頭
class Array
{
public:
int *x,sizem; //одно измерение, массив
// можно объявить здесь как **x , но это не то
Array();
~Array();
virtual void output();
};
class DArray:public Array
{
public:
int **z;
int sizen;
DArray();
~DArray();
virtual void output();
};
realisation.cpp
Array::Array()
{
sizem=5;
x=new int[sizem];
for(int i=0;i<sizem;i++)
x[i]=i;
}
Array::~Array()
{
delete []x;
}
void Array::output()
{
for(int i=0;i<sizem;cout<<x[i]<<" ",i++);
cout<<"\n";
}
void DArray::output()
{ cout<<"\n";
for(int i=0;i<sizem;i++)
{ for(int j=0;j<sizen;j++)
cout<<z[i][j]<<" ";
cout<<"\n";
}
}
DArray::DArray()
{
int i;
sizen=sizem;
z=new int*[sizem]; // одно измерение уже есть, т.е. это одна строка
for(i=1;i<sizem;i++)
z[i]=new int[sizem];
for(int i=1;i<sizem;i++) // if i don't initialization matrix, all'll
works right,compiles,and run, but AFAIK when i allocate space dynamically,
it must be intitializationed self by zeros, but it isn't particually equal
zeroes, the fisrt two column are equal some random number, other
are equal zero.
for(int j=0;j<sizen;j++)
{
z[i][j]=2;
}
*z=x; // i've already array x , that was inheritanced, just point address
}
DArray::~DArray()
{
for(int i=1;i<sizem;i++)
delete z[i];
// yet one problem
// i can't delete x e.g. delete []x
// but it should work!
delete []z;
}
PS我的工作沒有錯誤,但我認爲有內存泄漏,但無法趕上爲什麼
所有的招數已經構造已經完成,其他的一切都像輸出矩陣/陣列一般的東西。這就是爲什麼我認爲,問題是有
thanks you mates!
,但我不明白,但有一件事
z=new int*[sizem];
for(i=1;i<sizem;i++)
z[i]=new int[sizem];
for(int i=1;i<sizem;i++)
{
for(int j=0;j<sizen;j++)
cout<<z[i][j]<< " ";
cout<<"\n";
}
as i think i have to receive here ones zeros. byt i receive something like
2532336 2532336 0 0 0
2532336 2532336 0 0 0
2532336 2532336 0 0 0
2532336 2532336 0 0 0
爲什麼?
什麼是** x? йух? – Anycorn 2011-06-04 14:50:58
關於你在說什麼? – Tebe 2011-06-04 14:52:11
這是一個笑話,請忽略。 – Anycorn 2011-06-04 14:54:05