Как мне сделать так, чтобы размер матрицы и значение её элементов я вводил сам (то есть динамически)?
#pragma once
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
class Matrix {
int **matr;
int rows;
int cols;
public:
int *operator[](int k) {
return matr[k];
}
Matrix operator+(Matrix obj) {
Matrix tmp(0);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
tmp[i][j] = matr[i][j] + obj[i][j];
return tmp;
}
Matrix operator*(Matrix obj) {
Matrix tmp(0);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
tmp[i][j] += matr[i][k] * obj[k][j];
return tmp;
}
void show() {
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%3d", matr[i][j]);
}
cout << endl;
}
}
};
Добавить конструктор, который будет создавать матрицу указанного размера
Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.matr = new int*[rows];
for (int i = 0; i < rows; i++)
this.matr[i] = new int[cols];
}
Добавить деструктор, который будет уничтожать созданную матрицу
~Matrix(int rows, int cols) {
for (int i = 0; i < this.rows; i++)
delete [] this.matr[i];
delete [] this.matr;
}
И добавить конструктор копирования. Просто во избежание
Matrix(Matrix &matrix) {
this.rows = matrix.rows;
this.cols = matrix.cols;
this.matr = new int*[rows];
for (int i = 0; i < this.rows; i++) {
this.matr[i] = new int[cols];
for (int j = 0; j < this.cols; j++) {
this.matr[i][j] = matrix.matr[i][j];
}
}
}
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости