fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // 1) Інкапсуляція: поля були `protected`, що погано — їх треба зробити `private` і доступ надавати через методи.
  5. // 2) Конструктор без параметрів — створює матрицю 1x1.
  6. // 3) Деструктор реалізований.
  7. // 4) Перевантаження +
  8. // 5) Перевантаження -
  9. // 6) Обробка негативної розмірності
  10. // 7) Перевантаження *
  11. // 9) Клас SquareMatrix з методом trace()
  12.  
  13. using namespace std;
  14. // 1) що негаразд із кодом з погляду інкапсуляції? Як виправити?
  15. // 2) додайте можливість виклику конструктора без параметрів, щоб створювалася матриця розміру 1Х1
  16. //3) реалізувати деструктор
  17. // 4) перевантажити операцію + додавання 2х матриць
  18. // 5) перевантажити операцію - віднімання 2х матриць
  19. // 6) обробити створення матриці з негативною розмірністю
  20. // 7)перевантажити операцію * множення матриці на матрицю
  21. // 9) написати клас спадкоємець квадратна матриця, з методом обчислює слід матриці (сума елементів головної діаганалі)
  22.  
  23. template <typename T>
  24. class Matrix
  25. {
  26. protected:
  27. T **a;
  28. int size1;
  29.  
  30. int size2;
  31. public:
  32. Matrix(int n, int m){
  33. while(m<=0 and n<=0){
  34. cout<<"Enter a different size";
  35. cin>>n>>m;
  36. }
  37. size1=n;
  38. size2=m;
  39. a = new T * [n];
  40. for (int i = 0; i < n; i++){
  41. a[i] = new T [m];
  42. }
  43. }
  44.  
  45. T* operator [](const int i) {return a[i];}
  46. Matrix operator+(const Matrix& lhs)const{
  47. Matrix c(size1,size2);
  48. for(int i=0;i<size1;i++){
  49. for(int j=0;j<size2;j++){
  50. c.a[i][j]=a[i][j]+lhs.a[i][j];
  51. }
  52. }
  53. return c;
  54. }
  55. Matrix operator-(const Matrix& lhs)const{
  56. Matrix c(size1, size2);
  57. for(int i=0; i<size1; i++){
  58. for(int j=0; j<size2; j++){
  59. c[i][j]=a[i][j]-lhs.a[i][j];
  60. }
  61. }
  62. return c;
  63. }
  64. Matrix operator*(const Matrix& lhs){
  65. Matrix c(size1, size2);
  66. for(int i=0; i<size1; i++){
  67. for(int j=0; j<size2; j++){
  68. c[i][j]=a[i][j]*lhs.a[i][j];
  69. }
  70. }
  71. return c;
  72. }
  73. friend ostream& operator<< (ostream& os, const Matrix& a) {
  74. // countT++;
  75. for(int i=0;i<a.size1;i++){
  76. for(int j=0;j<a.size2;j++){
  77. os<<a.a[i][j]<<" ";
  78. }
  79. os<<endl;
  80. }
  81. return os;
  82. }
  83. friend istream& operator>> (istream& is, const Matrix& a) {
  84. // countT++;
  85. for(int i=0;i<a.size1;i++){
  86. for(int j=0;j<a.size2;j++){
  87. is>>a.a[i][j];
  88. }
  89. }
  90. return is;
  91. }
  92. ~Matrix(){
  93. for(int i=0; i<size1; i++){
  94. delete[] a[i];
  95. }
  96. delete[] a;
  97. cout<<"dest";
  98. }
  99. };
  100. template <typename T>
  101. class SquareMatrix: public Matrix<T>{
  102. public:
  103. SquareMatrix(int size): Matrix<T>(size, size){
  104.  
  105. }
  106. T trace(){
  107. T sum=0;
  108. for(int i=0; i<this->size1; i++){
  109. sum+=this->a[i][i];
  110. }
  111. return sum;
  112. }
  113. };
  114. template <typename T>
  115. class SquareM: public Matrix<T> {
  116. public:
  117. SquareM(int size) : Matrix<T>(size, size) {}
  118.  
  119. T trace() {
  120. T sum = 0;
  121. for (int i = 0; i < this->size1; i++)
  122. sum += this->a[i][i];
  123. return sum;
  124. }
  125. };
  126.  
  127. int main(void)
  128. {
  129. Matrix<double> a(2, 2);
  130. Matrix<double> b(2, 2);
  131.  
  132. cout << "Enter matrix A (2x2):\n";
  133. cin >> a;
  134. cout << "Enter matrix B (2x2):\n";
  135. cin >> b;
  136.  
  137. Matrix<double> c = a + b;
  138. cout << "\nMatrix A + B:\n" << c;
  139.  
  140. return 0;
  141. }
  142.  
  143.  
Success #stdin #stdout 0.01s 5284KB
stdin
2
4
5
3
stdout
Enter matrix A (2x2):
Enter matrix B (2x2):

Matrix A + B:
2 4 
5 3 
destdestdest