主题:C++中关于运算符重载的一个小问题
#include<iostream.h>
class Matrix
{
friend bool Multiply(Matrix &m1, Matrix &m2, Matrix &m3);
public:
Matrix(int row, int col);
~Matrix() {delete[] mem;}
void Print();
int GetRows() const {return rows;}
int GetCols() const {return cols;}
int & operator () (int i, int j); //"()"运算符的重载
bool operator = (Matrix &src); //赋值运算符"="的重
//Matrix operator = (Matrix &src);
Matrix operator * (Matrix &); //矩阵相乘
private:
int rows, cols;
int *mem;
};
Matrix::Matrix(int row, int col):rows(row), cols(col)
{
mem = new int[row * col];
}
int & Matrix::operator () (int i, int j)
{
return mem[i*cols + j];
}
bool Matrix::operator = (Matrix &src)
{
if (rows != src.rows || cols != src.cols)
{
return false;
}
for (int i = 0; i < rows * cols; i++)
{
mem[i] = src.mem[i];
}
return true;
}
Matrix Matrix::operator * (Matrix &m)
{
Matrix m3(rows, m.cols); //临时矩阵
int sum;
int i, j, k;
for (i = 0; i < rows; i++)
{
for (j = 0; j < m.cols; j++)
{
sum = 0;
for (k = 0; k < cols; k++)
{
sum += (*this)(i,k) * m(k,j);
}
m3(i,j) = sum;
}
}
return m3;
}
void Matrix::Print()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << mem[i*cols + j] << " ";
}
cout << endl;
}
cout << endl;
}
int main(int argc, char * argv)
{
Matrix m1(3,4), m2(4,3), m3(3, 3), m4(3, 3);
int i, j;
//设置m1内容
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
m1(i,j) = i + j;
}
}
//设置m2内容
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
{
m2(i, j) = i + j;
}
}
//m1 x m2 = m3
m3 = (m1*m2);//有什么问题,这一句????
m4 = m3;
m1.Print();
m2.Print();
m3.Print();
m4.Print();
return 0;
}
class Matrix
{
friend bool Multiply(Matrix &m1, Matrix &m2, Matrix &m3);
public:
Matrix(int row, int col);
~Matrix() {delete[] mem;}
void Print();
int GetRows() const {return rows;}
int GetCols() const {return cols;}
int & operator () (int i, int j); //"()"运算符的重载
bool operator = (Matrix &src); //赋值运算符"="的重
//Matrix operator = (Matrix &src);
Matrix operator * (Matrix &); //矩阵相乘
private:
int rows, cols;
int *mem;
};
Matrix::Matrix(int row, int col):rows(row), cols(col)
{
mem = new int[row * col];
}
int & Matrix::operator () (int i, int j)
{
return mem[i*cols + j];
}
bool Matrix::operator = (Matrix &src)
{
if (rows != src.rows || cols != src.cols)
{
return false;
}
for (int i = 0; i < rows * cols; i++)
{
mem[i] = src.mem[i];
}
return true;
}
Matrix Matrix::operator * (Matrix &m)
{
Matrix m3(rows, m.cols); //临时矩阵
int sum;
int i, j, k;
for (i = 0; i < rows; i++)
{
for (j = 0; j < m.cols; j++)
{
sum = 0;
for (k = 0; k < cols; k++)
{
sum += (*this)(i,k) * m(k,j);
}
m3(i,j) = sum;
}
}
return m3;
}
void Matrix::Print()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << mem[i*cols + j] << " ";
}
cout << endl;
}
cout << endl;
}
int main(int argc, char * argv)
{
Matrix m1(3,4), m2(4,3), m3(3, 3), m4(3, 3);
int i, j;
//设置m1内容
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
m1(i,j) = i + j;
}
}
//设置m2内容
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
{
m2(i, j) = i + j;
}
}
//m1 x m2 = m3
m3 = (m1*m2);//有什么问题,这一句????
m4 = m3;
m1.Print();
m2.Print();
m3.Print();
m4.Print();
return 0;
}