|
| 1 | +/* |
| 2 | +
|
| 3 | +Class Matrix |
| 4 | +{ |
| 5 | + int matrix[3][3],rows=3,cols=3; |
| 6 | +}; |
| 7 | +
|
| 8 | +Let m1 and m2 are two matrices. Find out m3=m1+m2 (use operator |
| 9 | +overloading). |
| 10 | +
|
| 11 | +*/ |
| 12 | + |
| 13 | +// // Header files |
| 14 | +#include <iostream> |
| 15 | +#include <conio.h> |
| 16 | +#include <iomanip> |
| 17 | + |
| 18 | +// // use namespace |
| 19 | +using namespace std; |
| 20 | + |
| 21 | +// // define class Matrix |
| 22 | +class Matrix |
| 23 | +{ |
| 24 | + |
| 25 | +private: |
| 26 | + // // instance member variables |
| 27 | + int matrix[3][3], rows = 3, cols = 3; |
| 28 | + |
| 29 | +public: |
| 30 | + // // constructors |
| 31 | + Matrix() |
| 32 | + { |
| 33 | + } |
| 34 | + |
| 35 | + // // instance member function to input matrix |
| 36 | + void inputMatrix() |
| 37 | + { |
| 38 | + for (int i = 0; i < rows; i++) |
| 39 | + { |
| 40 | + for (int j = 0; j < cols; j++) |
| 41 | + { |
| 42 | + cout << "\nEnter element[" << i << "][" << j << "] => "; |
| 43 | + cin >> matrix[i][j]; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // // instance member function to show matrix |
| 49 | + void showMatrix() |
| 50 | + { |
| 51 | + for (int i = 0; i < rows; i++) |
| 52 | + { |
| 53 | + for (int j = 0; j < cols; j++) |
| 54 | + cout << setw(4) << matrix[i][j]; |
| 55 | + |
| 56 | + cout << endl; // // Add New line |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // // instance member function to set element |
| 61 | + void setElement(int row, int col, int value) |
| 62 | + { |
| 63 | + if (row >= 0 && row < rows && col >= 0 && col < cols) |
| 64 | + matrix[row][col] = value; |
| 65 | + else |
| 66 | + cout << "\nInvalid Index..."; |
| 67 | + } |
| 68 | + |
| 69 | + // // instance member function to get element |
| 70 | + int getElement(int row, int col) |
| 71 | + { |
| 72 | + if (row >= 0 && row < rows && col >= 0 && col < cols) |
| 73 | + return matrix[row][col]; |
| 74 | + cout << "\nInvalid Index..."; |
| 75 | + } |
| 76 | + |
| 77 | + // // overload binary plus (+) operator |
| 78 | + Matrix operator+(Matrix m2) |
| 79 | + { |
| 80 | + Matrix temp; |
| 81 | + for (int i = 0; i < rows; i++) |
| 82 | + { |
| 83 | + for (int j = 0; j < cols; j++) |
| 84 | + temp.matrix[i][j] = matrix[i][j] + m2.matrix[i][j]; |
| 85 | + } |
| 86 | + return temp; |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +// // Main Function Start |
| 91 | +int main() |
| 92 | +{ |
| 93 | + |
| 94 | + Matrix matrixA, matrixB, matrixC; // // create objects of Matrix class |
| 95 | + |
| 96 | + cout << "\n>>>>>>>> Enter Two Matrices to Add <<<<<<<<<\n"; |
| 97 | + |
| 98 | + // // input elements of matrixA |
| 99 | + cout << "\n>>>>>>>> Enter Elements of Matrix-A of Order 3 x 3 <<<<<<<<<\n"; |
| 100 | + matrixA.inputMatrix(); |
| 101 | + |
| 102 | + // // input elements of matrixB |
| 103 | + cout << "\n>>>>>>>> Enter Elements of Matrix-B of Order 3 x 3 <<<<<<<<<\n"; |
| 104 | + matrixB.inputMatrix(); |
| 105 | + |
| 106 | + // // show matrixA |
| 107 | + cout << "\n>>>>>>>> Matrix-A <<<<<<<<<\n"; |
| 108 | + matrixA.showMatrix(); |
| 109 | + |
| 110 | + // // show matrixB |
| 111 | + cout << "\n>>>>>>>> Matrix-B <<<<<<<<<\n"; |
| 112 | + matrixB.showMatrix(); |
| 113 | + |
| 114 | + // // add matrices |
| 115 | + matrixC = matrixA + matrixB; |
| 116 | + |
| 117 | + // // show matrixC result of addition |
| 118 | + cout << "\n>>>>>>>> Result of Addition Matrix-C <<<<<<<<<\n"; |
| 119 | + matrixC.showMatrix(); |
| 120 | + |
| 121 | + cout << endl; // Add new line |
| 122 | + getch(); |
| 123 | + return 0; |
| 124 | +} |
| 125 | +// // Main Function End |
0 commit comments