C++ Online Compiler
Example: Matrix Addition using Class Example in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Matrix Addition using Class Example #include <iostream> #include <vector> // Required for std::vector using namespace std; // Using namespace std as per instructions // Define the Matrix class class Matrix { private: int rows; int cols; vector<vector<int>> data; // Using std::vector for dynamic 2D array public: // Constructor to initialize matrix dimensions and resize data vector Matrix(int r, int c) : rows(r), cols(c) { if (r <= 0 || c <= 0) { cerr << "Error: Matrix dimensions must be positive." << endl; this->rows = 0; // Indicate an invalid matrix this->cols = 0; } else { data.resize(rows, vector<int>(cols)); } } // Method to get matrix elements from the user void input() { if (rows == 0 || cols == 0) { cerr << "Cannot input into an uninitialized or invalid matrix." << endl; return; } cout << "Enter elements for a " << rows << "x" << cols << " matrix:" << endl; for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { cout << "Enter element at position [" << i + 1 << "][" << j + 1 << "]: "; cin >> data[i][j]; } } } // Method to display the matrix elements void display() const { if (rows == 0 || cols == 0) { cout << "Matrix is empty or invalid." << endl; return; } for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { cout << data[i][j] << "\t"; } cout << endl; } } // Method to add two matrices Matrix add(const Matrix& other) const { // Check if dimensions are compatible for addition if (rows != other.rows || cols != other.cols) { cerr << "Error: Matrices must have the same dimensions for addition." << endl; return Matrix(0, 0); // Return an invalid matrix } // Create a new matrix to store the result of the addition Matrix result(rows, cols); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { result.data[i][j] = this->data[i][j] + other.data[i][j]; } } return result; } // Getter methods for dimensions (useful for external checks) int getRows() const { return rows; } int getCols() const { return cols; } }; int main() { // Step 1: Get dimensions for the first matrix from the user int r1, c1; cout << "Enter the number of rows for Matrix 1: "; cin >> r1; cout << "Enter the number of columns for Matrix 1: "; cin >> c1; // Step 2: Create Matrix 1 object and get its elements Matrix mat1(r1, c1); mat1.input(); // Step 3: Get dimensions for the second matrix from the user int r2, c2; cout << "\nEnter the number of rows for Matrix 2: "; cin >> r2; cout << "Enter the number of columns for Matrix 2: "; cin >> c2; // Step 4: Create Matrix 2 object and get its elements Matrix mat2(r2, c2); mat2.input(); // Step 5: Display Matrix 1 cout << "\n--- Matrix 1 ---" << endl; mat1.display(); // Step 6: Display Matrix 2 cout << "\n--- Matrix 2 ---" << endl; mat2.display(); // Step 7: Check if matrices can be added and perform addition if (mat1.getRows() == mat2.getRows() && mat1.getCols() == mat2.getCols()) { Matrix sumMat = mat1.add(mat2); cout << "\n--- Sum of Matrices ---" << endl; sumMat.display(); } else { cout << "\nError: Cannot add matrices. Dimensions mismatch!" << endl; } return 0; }
Output
Clear
ADVERTISEMENTS