C++ Online Compiler
Example: Matrix Addition using Class in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Matrix Addition using Class #include <iostream> using namespace std; class Matrix { private: int rows; int cols; int** data; // Pointer to a pointer for 2D array public: // Constructor Matrix(int r, int c) : rows(r), cols(c) { // Step 1: Allocate memory for rows data = new int*[rows]; // Step 2: Allocate memory for columns for each row for (int i = 0; i < rows; ++i) { data[i] = new int[cols]; } } // Destructor to free allocated memory ~Matrix() { // Step 1: Free memory for columns in each row for (int i = 0; i < rows; ++i) { delete[] data[i]; } // Step 2: Free memory for rows delete[] data; } // Method to input matrix elements void inputMatrix() { cout << "Enter matrix elements (" << rows << "x" << cols << "):" << endl; for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { cout << "Enter element at (" << i + 1 << "," << j + 1 << "): "; cin >> data[i][j]; } } } // Method to display matrix elements void displayMatrix() const { // const to indicate it doesn't modify the object cout << "Matrix (" << rows << "x" << cols << "):" << endl; for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { cout << data[i][j] << "\t"; } cout << endl; } } // Overload the + operator for matrix addition Matrix operator+(const Matrix& other) const { // Step 1: Check if matrices have compatible dimensions for addition if (rows != other.rows || cols != other.cols) { cout << "Error: Matrices must have the same dimensions for addition." << endl; // Return a default matrix or throw an exception return Matrix(0, 0); // Returning an empty matrix as an error indication } // Step 2: Create a new matrix to store the result Matrix result(rows, cols); // Step 3: Perform element-wise addition 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; } // Optional: Getter methods for dimensions if needed externally int getRows() const { return rows; } int getCols() const { return cols; } }; int main() { int r1, c1, r2, c2; // Step 1: Get dimensions for the first matrix cout << "Enter dimensions for Matrix 1 (rows columns): "; cin >> r1 >> c1; Matrix mat1(r1, c1); mat1.inputMatrix(); // Step 2: Get dimensions for the second matrix cout << "\nEnter dimensions for Matrix 2 (rows columns): "; cin >> r2 >> c2; Matrix mat2(r2, c2); mat2.inputMatrix(); // Step 3: Display original matrices cout << "\n--- Matrices Entered ---" << endl; mat1.displayMatrix(); mat2.displayMatrix(); // Step 4: Perform matrix addition using overloaded operator // Only perform addition if dimensions match for the current example setup // (Error handling for dimension mismatch is inside operator+ and returns (0,0) matrix) if (r1 == r2 && c1 == c2) { Matrix matSum = mat1 + mat2; // Calls the overloaded operator+ cout << "\n--- Resultant Matrix (Sum) ---" << endl; matSum.displayMatrix(); } else { cout << "\nCannot add matrices: Dimensions mismatch." << endl; } return 0; }
Output
Clear
ADVERTISEMENTS