![]() |
#2
寒风中的细雨2011-04-11 21:48
![]() #include <iostream> #include <cstdio> #include <memory> #include <windows.h> using namespace std; class matrix { int *m_elems; short m_rows, m_cols; public: matrix(short rows, short cols); ~matrix(); int *operator[] (short row); friend void PrintMatrix(matrix m); matrix(const matrix &p); void operator = (matrix p); }; matrix::matrix(short rows, short cols) { m_rows = rows; m_cols = cols; m_elems = new int[rows*cols]; memset(m_elems, 0, sizeof(int)*rows*cols); } matrix::~matrix() { if (NULL != m_elems) { delete [] m_elems; m_elems = NULL; } } int * matrix::operator [](short row) { static i = 0; cout << ++i << endl; return m_elems + row * m_cols; } void PrintMatrix(matrix m) { for (int i=0; i<m.m_rows; ++i) { for (int k=0; k < m.m_cols; ++k) { cout << m.m_elems[i*m.m_cols+k] << " "; } cout << endl; } cout << endl; } matrix::matrix(const matrix &p) { m_rows = p.m_rows; m_cols = p.m_cols; m_elems = new int[p.m_cols*p.m_rows]; memcpy(m_elems, p.m_elems, sizeof(int)*p.m_cols*p.m_rows); } void matrix::operator =(matrix p) { m_rows = p.m_rows; m_cols = p.m_cols; m_elems = new int[p.m_cols*p.m_rows]; memcpy(m_elems, p.m_elems, sizeof(int)*p.m_cols*p.m_rows); } int main() { matrix a(2, 3); a[0][0] = 1; a[0][1] = 2; a[0][2] = 3; a[1][0] = 4; a[1][1] = 5; a[1][2] = 6; PrintMatrix(a); system("pause"); return 0; } |
#include <conio.h>
#include <memory.h>
#include <iostream>
#include "stdio.h"
using namespace std;
class matrix{
short rows, cols;
int *elems;
public:
matrix(short rows, short cols); // 构造函数
~matrix(); // 析构函数
// 重载'[]'以支持下标操作
int* operator[] (short row);
// 添加友元函数打印矩阵
friend void PrintMatrix(matrix m);
matrix(const matrix & p); // 拷贝构造函数
void operator = (matrix p); // 重载'='实现矩阵之间的赋值
};
//---------------------------------------------类实现----------------------------------------------
// 构造函数,分配内存
matrix::matrix(short rows, short cols)
{
matrix::rows = rows;
matrix::cols = cols;
elems = new int[rows * cols];
// 初始化所有值为0
memset(elems, 0, sizeof(int) * rows * cols);
}
// 析构函数,释放内存
matrix::~matrix(){
if( elems != NULL){
delete []elems;
elems = NULL;
}
}
// 重载'[]'以支持下标运算
int* matrix::operator[](short row)
{
// 逻辑上,在类matrix的外部使用者看来它是一个二维矩阵,
// 所以'[]'运算应该返回一个一维数组的首地址,其实是个指针
return elems + row * cols;
}
// 输出矩阵各元素的值
void PrintMatrix(matrix m)
{
for(int i = 0; i < m.rows; i++){
for(int k = 0; k < m.cols; k++){
// cout << m.elems[i * m.cols + k] << " ";
}
// cout << endl;
}
// cout << endl;
}
// 重写拷贝构造函数
matrix::matrix(const matrix & p)
{
rows = p.rows;
cols = p.cols;
elems = new int[p.rows * p.cols];
memcpy( elems, p.elems, sizeof(double) * p.rows * p.cols);
}
// 重载'='实现矩阵之间的赋值
void matrix::operator = (matrix p)
{
rows = p.rows;
cols = p.cols;
elems = new int[p.rows * p.cols];
memcpy( elems, p.elems, sizeof(double) * p.rows * p.cols);
}
int main(int argc, char* argv[])
{
matrix a(2,3);
PrintMatrix(a);
a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 4;
a[1][1] = 5;
a[1][2] = 6;
PrintMatrix(a);
getch();
return 0;
}
上面的代码用F5可以运行,但使用Ctrl+F5却出现了XX遇到问题需要关闭,求解释。。。