分享知识

与大家一起分享知识

C++博客 首页 新随笔 联系 聚合 管理
  19 Posts :: 3 Stories :: 45 Comments :: 0 Trackbacks

希望大家能够一起讨论,把矩阵这个类给完善。

#include  <iostream>
using namespace std;

class  Matrix
{
private:
    int  rows,columns;
    int **pMatrix;
public: 
    Matrix(int rows = 3,int columns = 2);
    Matrix(const Matrix &M);
    ~Matrix();
    Matrix& operator=(const Matrix& M);
    int  GetRows() const;
    int  GetColumns() const;
    void  SetValue();
    friend Matrix operator*(const Matrix& a,const Matrix& b);
    friend ostream& operator<<(ostream& os,const Matrix& M);
};

int  Matrix::GetRows() const  { return  rows;}
int  Matrix::GetColumns() const { 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];
        for(int j = 0;j < y;j++)   //初始化每个值为0
            pMatrix[i][j] = 0;
    }
}
// 析构函数
Matrix::~Matrix()
{
    for (int  i = 0 ;i < rows;i ++ )
        delete[] pMatrix[i];
    delete[] pMatrix;
}

// 赋值函数
Matrix& Matrix::operator=(const Matrix& M)
{
 if(this != &M)
 {
  for (int  ii = 0 ;ii < rows;ii++ )
   if(pMatrix[ii])
    delete[] pMatrix[ii];
  if(pMatrix)
   delete[] pMatrix;
    rows = M.rows;
    columns = M.columns;
  //分配存储空间
  pMatrix = new int* [rows];
  for (int k = 0 ; k < rows; k++ )
   pMatrix[k] = new int[columns];

  for ( int  i = 0 ; i < rows; i ++ )
   for ( int  j = 0 ; j < columns; j ++ )
    pMatrix[i][j] = M.pMatrix[i][j];
 }
 return *this;
}
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(const Matrix&  M)

 rows = M.rows;
 columns = M.columns;
 //分配存储空间
 pMatrix = new int* [rows];
 for (int k = 0 ; k < rows; k++ )
  pMatrix[k] = new int[columns];

 for ( int  i = 0 ; i < rows; i ++ )
  for ( int  j = 0 ; j < columns; j ++ )
   pMatrix[i][j] = M.pMatrix[i][j];
}

Matrix operator*(const Matrix& a,const Matrix& b)
{
 if  (a.columns == b.rows)
 {
  Matrix c(a.rows,b.columns);
  for ( int  i = 0 ;i < a.rows;i ++ )
  {
   for ( int  j = 0 ;j < b.columns;j ++ ) 
   {  
    for ( int  columnIndex= 0 ;columnIndex < a.columns;columnIndex++ )
     c.pMatrix[i][j] += a.pMatrix[i][columnIndex] * b.pMatrix[columnIndex][j];
   }
  }
  return c;
 }
 else
  return Matrix();
}

ostream& operator<<(ostream& os,const Matrix& M)
{
 for (int i = 0;i < M.rows;i++ )
 {
  for (int j = 0;j < M.columns;j++ )
   os << M.pMatrix[i][j] << "  ";
  os << endl;
 }
 return (os << endl);
}


// 主函数
void  main()
{
 Matrix Ma(3,2),Mb(2,2);
 Ma.SetValue();
 Mb.SetValue();
 cout << Ma << endl;
 cout << Mb << endl;
 
 Matrix Mc = Ma * Mb;//拷贝构造函数
 cout << Mc << endl;
 Mc = Mb;     //=运算符,即赋值函数
 cout << Mb << endl;
}

posted on 2006-04-12 13:56 史传红 阅读(518) 评论(2)  编辑 收藏 引用 所属分类: C/C++细节知识

Feedback

# re: 实现(关于《[求助] 关于拷贝构造函数,对象传递!!》这篇文章) 2006-04-12 16:53 任我行
Matrix operator*(const Matrix& a,const Matrix& b)
感觉还有问题。
a,b的row,column 不相等没有考虑。
  回复  更多评论
  

# re: 实现(关于《[求助] 关于拷贝构造函数,对象传递!!》这篇文章) 2006-04-12 18:13 芋头
矩阵运算,对另一矩阵维度有要求,而这个维度是可以在编译期确定的。这样的话,我推荐把它写成泛型的,方便在编译期检查错误。比如点乘,你这里判断维度如果不正确,就构造一个默认的返回去,实际上应该报错才好。泛型可以解决这个问题。
template <int C, int R>
class Matrix
{
...
};

template <int C, int R, int R1>
Matrix<C, R1> operator*(const Matrix<C, R>& lhs, const Matrix<R, R1>& rhs);

大致的原型就是这样吧,可能要加几个typename。这样写,不符合要求的肯定乘不到一块去,会提示乘操作未声明。

泛型还可以解决动态分配内存的问题,它可以做到在栈上分配,大小都可以在编译期决议。当然占用栈空间比较多,写成堆上分配也可以。  回复  更多评论
  


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