Oops15.pdf
Oops15.pdf
Program - Write a C++ program for developing a matrix class which can handle
integer matrices of different dimensions. Also overload the operator for
addition, multiplication & comparison of matrices.
Code
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
class Matrix {
private:
vector<int> elements;
int rows;
int cols;
public:
Matrix(int r, int c) {
this -> rows = r;
this -> cols = c;
elements.resize(rows * cols);
}
void insertElements() {
int x;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << "Enter the element at Index (" << i << "," << j << ") : ";
cin >> x;
elements[i * cols + j] = x; // Set the value at computed index
}
}
}
if (m1 == m2) {
cout << "The matrices are equal." << endl;
} else {
cout << "The matrices are not equal." << endl;
}
return 0;
}
Output