⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix.cpp

📁 一些C++的课件和实验源代码
💻 CPP
字号:

#include "stdafx.h"
#include "matrix.h"
#include <memory.h>
#include <iostream>
using namespace std;

// 构造函数,分配内存
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);
}
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -