华剑缘
一切都在这个过程中获得,将那些目标埋藏于心中
posts - 19,comments - 20,trackbacks - 0

实验 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 华剑缘 阅读(1343) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理