/*Создать класс для работы с матрицами.
Предусмотреть, как минимум, функции для
сложения матриц, умножения матриц,
транспонирования матриц, присваивания
матриц друг другу, установка и получение
произвольного элемента матрицы. Необходимо
перегрузить соответствующие операторы.
*/
#include <iostream>
#include <iomanip>
using namespace std;
class Cmatrix
{
private:
short **mas;
short size;
public:
Cmatrix()//конст по умолч
{
size = 3;
mas = new short * [size];
for (short i = 0; i < size; i++)
mas[i] = new short [size];
short count(1);
for (short i = 0; i < size; i++)
for (short j = 0; j < size; j++, count++)
mas[i][j] = count;
}
//перегрузка +
Cmatrix operator + (Cmatrix &B)
{
for (short i = 0; i < size; i++)
{
for (short j = 0; j < size; j++)
{
mas[i][j] += B.mas[i][j];
}
}
return *this;
}
//перегрузка *
Cmatrix operator * (Cmatrix &B)
{
for (short i = 0; i < size; i++)
{
for (short j = 0; j < size; j++)
{
mas[i][j] *= B.mas[i][j];
}
}
return *this;
}
//перегрузка !=
Cmatrix& operator != (Cmatrix &C)
{
short** newmas;//--------создание временного массива------------
newmas = new short* [size];
for (short i = 0; i < size; i++)
newmas[i] = new short [size];
for (short i =0 ; i < size; i++)
for (short j = 0; j < size; j++)
newmas[i][j] = C.mas[i][j];//---------------------------
for (short i = 0; i < size; i++)//строка
for (short j = 0; j < size; j++)//столбец
C.mas[i][j] = newmas[j][i];//копирование
for (short i = 0; i < size; i++)//удаление временного массива
delete newmas[i];
delete [] newmas;
return C;
}
//перегрузка ==
Cmatrix operator == (Cmatrix &A)
{
for (short i = 0; i < this->size; i++)
for (short j = 0; j < this->size; j++)
mas[i][j] = A.mas[i][j];
return *this;
}
//перегрузка == 2
Cmatrix operator == (const int &X)
{
mas[2][1] = X;
return *this;
}
//перегрузка <
Cmatrix operator ++ ()
{
cout<<endl<<setw(4)<<this->mas[1][1]<<endl;
return *this;
}
friend ostream& operator<<(ostream& co, const Cmatrix& X);
~Cmatrix()
{
cout<<"destruct\n";
}
};
//перегрузка вывода <<
ostream& operator << (ostream&stream, const Cmatrix &X)
{
for (short i = 0; i < X.size; i++)
{
cout<<endl;
for (short j = 0; j < X.size; j++)
stream<<setw(4)<<X.mas[i][j];
}
stream<<endl;
return stream;
}
void main()
{
Cmatrix A;
Cmatrix B;
Cmatrix C;
cout<<B;
C = B + A;
cout<<C;
C = B * A;
cout<<C;
C!=C;
cout<<C;
C == A;
cout<<C;
C == 15;
cout<<C;
C++;
cout<<endl<<endl;
}