實驗
3
對象數組與對象指針
1
.實驗目的
(1)
掌握數組與指針的定義與使用方法。
(2)
理解數組與指針的存儲分配與表示。
(3)
學習向函數傳遞數組的方法。
(4)
學習用指針和引用向函數傳遞參數。
(5)
學習靜態數據成員和靜態成員函數的使用。
(6)
理解友元與友元函數的作用與使用方法。
2
.實驗基本要求
(1)
設計一個矩陣類
Matrix(
矩陣由二維數組實現
)
,有分配空間和對矩陣賦值的功能。
(2)
練習將這個矩陣類的對象作為參數傳送到函數
Mul()
,用普通、指針和引用三種方法實現,并要注意這三種方式的區別。
???
①直接傳送:
Mul(Matrix a
,
Matrix b)
。實際上只是傳送值,在函數中針對對象的任何修改均不影響該對象本身。
???
②指針傳送:
Mul(Matrix *pa
,
Matrix *pb)
。要注意指針的級數。
???
③引用傳送:
Mul(Matrix & a
,
Matrix & b)
。這種調用將影響參數的實際值。
(3)
將
Mul()
函數實現:完成對傳送的兩個
Matrix
對象的相乘運算。下面給出矩陣相乘的算法:
???
①矩陣
a[i][j]
與矩陣
b[x][y]
相乘,條件是
j==x
。
???
②乘積是一個新的矩陣
c[i][y]
,其中
c[i][y]
的值是∑
(a[i][k]* b[k][y])
,其中
K
=
0,l,…,j
(4)
在
Matrix
類中定義一個靜態數據成員,記錄當前的所有
Matrix
對象的數量。
(5)
定義一個友元函數實現轉置的功能。轉置是指將數組中
a[i][j]
與
a[j] [i]
的值對調。
3
.實驗基本步驟
(1)
建立一個工程。在工程中定義一個
Matrix
類,在構造函數中根據參數創建數據成員:一個二維數組。提示:用構造函數記錄二維數組的大小
(unsigned int x
,
unsigned int y)
。類中實際定義的二維數組的數據成員是一個指針
(
二級指針
)
,
int **pMatrix
。
在構造函數中根據傳送的參數為這個二維數組分配空間:
pMatrix
=
newint[x][y]
。
(2)
設計成員函數,完成對數組的賦值的功能。本例中定義的成員函數為
SetValue(unsigned int x
,
unsigned int y
,
int value)
。
(3)
參考以上的說明,以常用三種方式實現向
Mul()
函數傳送參數,并返回矩陣相乘的結果。
(4)
在類中定義一個靜態的數據成員
ObjectAliveNo
,記錄當前共有幾個
Matrix
類的對象。實現方法:可以在對象的構造函數中向該數據成員報告
(
使靜態數據成員加
1)
;在析構函數中也向該數據成員報告
(
使靜態數據成員減
1)
。并要注意,在程序開始時,給這個靜態數據成員賦初值。
(5)
在
Matrix
類中定義一個友元函數,使其具有對
Matrix
類的對象內的數組進行轉置的功能。
?
#include?
<
iostream
>
using
?
namespace
?std;
class
?Matrix

{
private
:
????
int
?rows,columns;
public
:
int
?
**
pMatrix;
Matrix(?
int
?rows,
int
?columns);
Matrix(Matrix
&
?);
~
Matrix();
int
?GetRows();
int
?GetColumns();
void
?SetValue();
void
?Mul(Matrix?
&
?a,Matrix?
&
?b);


}
;


int
?Matrix::GetRows()
{
return
?rows;}
;

int
?Matrix::GetColumns()
{
return
?columns;}
;

//
構造函數
Matrix::Matrix(
int
?x,
int
?y)

{
rows
=
x;
columns
=
y;
pMatrix
=
new
?
int
*
?[x];
for
(
int
?i
=
0
;?i
<
x;?i
++
)
??pMatrix[i]
=
new
?
int
?[y];
}
//
析構函數
Matrix::
~
Matrix()

{
for
(
int
?i
=
0
;i
<
columns;i
++
)
??delete?[]pMatrix[i];
????delete[]?pMatrix;
}
//
賦值函數
void
?Matrix::SetValue()

{
????
int
?i,j,value;
????
for
(?i
=
0
;?i
<
rows;?i
++
)

{
??
for
(?j
=
0
;?j
<
columns;?j
++
)

??
{
???cout
<<
"
第
"
<<
i
<<
"
行
"
;
???cout
<<
"
第
"
<<
j
<<
"
列:
"
;
???cin
>>
value;
???cout
<<
endl;
???pMatrix[i][j]
=
value;
??}
}
}
//
拷貝構造函數
Matrix::Matrix(Matrix
&
?M)

{
for
(
int
?i
=
0
;?i
<
M.rows;?i
++
)
??
for
(
int
?j
=
0
;?j
<
M.columns;?j
++
)

???pMatrix[i][j]
=
M.pMatrix[i][j];?????
/**/
///
這樣對么?
}
void
?Matrix::Mul(?Matrix?
&
?a,?Matrix?
&
?b)

{
Matrix?c(a.GetRows(),b.GetColumns());
int
?temp
=
0
;
????
for
(
int
?ai
=
0
;ai
<
a.GetRows();ai
++
)

{

??
for
(
int
?bj
=
0
;bj
<
b.GetColumns();bj
++
)

??
{
???
for
(
int
?aj
=
0
;aj
<
a.GetColumns();aj
++
)
????temp
=
temp
+
a.pMatrix[ai][aj]
*
b.pMatrix[aj][bj];
???c.pMatrix[ai][bj]
=
temp;
???temp
=
0
;
??}
}
for
(
int
?i
=
0
;i
<
c.GetRows();i
++
)
{
//
輸出相乘后的矩陣
???cout
<<
'
\n
'
;
??
for
(
int
?j
=
0
;j
<
c.GetColumns();j
++
)
???cout
<<
c.pMatrix[i][j]
<<
"
??
"
;
?}
}
//
主函數
int
?main()

{
Matrix?Ma(
2
,
2
),Mb(
2
,
2
);
Ma.SetValue();
Mb.SetValue();

for
(
int
?i
=
0
;i
<
Ma.GetRows();i
++
)

{
????cout
<<
'
\n
'
;
??
for
(
int
?j
=
0
;j
<
Ma.GetColumns();j
++
)
???cout
<<
Ma.pMatrix[i][j]
<<
"
?
"
;
}
cout
<<
endl;
Matrix?Mc(
2
,
2
);
Mc.Mul(Ma,Mb);

/**/
/*
for(int?i=0;i<Mc.GetRows();i++)
{
????cout<<'\n';
??for(int?j=0;j<Mc.GetColumns();j++)
???cout<<Mc.pMatrix[i][j]<<"?";
}
*/
return
?
0
;
}
posted on 2006-04-03 16:21
華劍緣 閱讀(1367)
評論(0) 編輯 收藏 引用