fork(71) download
  1. /*Создать класс для работы с матрицами.
  2. Предусмотреть, как минимум, функции для
  3. сложения матриц, умножения матриц,
  4. транспонирования матриц, присваивания
  5. матриц друг другу, установка и получение
  6. произвольного элемента матрицы. Необходимо
  7. перегрузить соответствующие операторы.
  8. */
  9. #include <iostream>
  10. #include <iomanip>
  11. using namespace std;
  12.  
  13. class Cmatrix
  14. {
  15. private:
  16. short **mas;
  17. short size;
  18.  
  19. public:
  20. Cmatrix()//конст по умолч
  21. {
  22. size = 3;
  23. mas = new short * [size];
  24. for (short i = 0; i < size; i++)
  25. mas[i] = new short [size];
  26.  
  27. short count(1);
  28. for (short i = 0; i < size; i++)
  29. for (short j = 0; j < size; j++, count++)
  30. mas[i][j] = count;
  31.  
  32. }
  33. //перегрузка +
  34. Cmatrix operator + (Cmatrix &B)
  35. {
  36. for (short i = 0; i < size; i++)
  37. {
  38. for (short j = 0; j < size; j++)
  39. {
  40. mas[i][j] += B.mas[i][j];
  41. }
  42. }
  43. return *this;
  44. }
  45. //перегрузка *
  46. Cmatrix operator * (Cmatrix &B)
  47. {
  48. for (short i = 0; i < size; i++)
  49. {
  50. for (short j = 0; j < size; j++)
  51. {
  52. mas[i][j] *= B.mas[i][j];
  53. }
  54. }
  55. return *this;
  56. }
  57. //перегрузка !=
  58. Cmatrix& operator != (Cmatrix &C)
  59. {
  60. short** newmas;//--------создание временного массива------------
  61. newmas = new short* [size];
  62. for (short i = 0; i < size; i++)
  63. newmas[i] = new short [size];
  64.  
  65. for (short i =0 ; i < size; i++)
  66. for (short j = 0; j < size; j++)
  67. newmas[i][j] = C.mas[i][j];//---------------------------
  68.  
  69.  
  70. for (short i = 0; i < size; i++)//строка
  71. for (short j = 0; j < size; j++)//столбец
  72. C.mas[i][j] = newmas[j][i];//копирование
  73.  
  74. for (short i = 0; i < size; i++)//удаление временного массива
  75. delete newmas[i];
  76. delete [] newmas;
  77.  
  78. return C;
  79. }
  80. //перегрузка ==
  81. Cmatrix operator == (Cmatrix &A)
  82. {
  83. for (short i = 0; i < this->size; i++)
  84. for (short j = 0; j < this->size; j++)
  85. mas[i][j] = A.mas[i][j];
  86. return *this;
  87. }
  88. //перегрузка == 2
  89. Cmatrix operator == (const int &X)
  90. {
  91. mas[2][1] = X;
  92. return *this;
  93. }
  94. //перегрузка <
  95. Cmatrix operator ++ ()
  96. {
  97. cout<<endl<<setw(4)<<this->mas[1][1]<<endl;
  98. return *this;
  99. }
  100.  
  101.  
  102. friend ostream& operator<<(ostream& co, const Cmatrix& X);
  103.  
  104. ~Cmatrix()
  105. {
  106. cout<<"destruct\n";
  107. }
  108.  
  109. };
  110. //перегрузка вывода <<
  111. ostream& operator << (ostream&stream, const Cmatrix &X)
  112. {
  113. for (short i = 0; i < X.size; i++)
  114. {
  115. cout<<endl;
  116. for (short j = 0; j < X.size; j++)
  117. stream<<setw(4)<<X.mas[i][j];
  118. }
  119. stream<<endl;
  120. return stream;
  121. }
  122.  
  123.  
  124.  
  125. void main()
  126. {
  127. Cmatrix A;
  128. Cmatrix B;
  129. Cmatrix C;
  130. cout<<B;
  131. C = B + A;
  132. cout<<C;
  133. C = B * A;
  134. cout<<C;
  135. C!=C;
  136. cout<<C;
  137. C == A;
  138. cout<<C;
  139. C == 15;
  140. cout<<C;
  141. C++;
  142.  
  143. cout<<endl<<endl;
  144. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:125: error: ‘::main’ must return ‘int’
prog.cpp: In function ‘int main()’:
prog.cpp:141: error: no ‘operator++(int)’ declared for postfix ‘++’, trying prefix operator instead
stdout
Standard output is empty