BCA
Object Oriented Programming using C++ Practical
Madras University
Program 6
Design a 3x3 matrix class and demonstrate the following:
a. Addition and multiplication of two matrices using operator overloading
b. Maintaining a count of the number of matrix object created.
SOURCE CODE:
#include <iostream>
using namespace std;
class Matrix3x3 {
private:
int data[3][3];
static int objectCount; // Static variable to count the number of objects
public:
// Constructor to initialize the matrix
Matrix3x3(int init[3][3]) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
data[i][j] = init[i][j];
}
}
objectCount++;
}
// Default constructor
Matrix3x3() {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
data[i][j] = 0;
}
}
objectCount++;
}
// Destructor
~Matrix3x3() {
objectCount--;
}
// Static function to get the number of objects
static int getObjectCount() {
return objectCount;
}
// Operator overloading for matrix addition
Matrix3x3 operator+(const Matrix3x3& other) const {
Matrix3x3 result;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
result.data[i][j] = data[i][j] + other.data[i][j];
}
}
return result;
}
// Operator overloading for matrix multiplication
Matrix3x3 operator*(const Matrix3x3& other) const {
Matrix3x3 result;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
result.data[i][j] = 0;
for (int k = 0; k < 3; ++k) {
result.data[i][j] += data[i][k] * other.data[k][j];
}
}
}
return result;
}
// Function to read matrix from user input
void readFromUser() {
cout << "Enter the elements of the 3x3 matrix (row-wise):" << endl;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cin >> data[i][j];
}
}
}
// Function to print the matrix
void print() const {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
};
// Initialize static member
int Matrix3x3::objectCount = 0;
int main() {
Matrix3x3 matrix1, matrix2;
cout << "Enter matrix 1:" << endl;
matrix1.readFromUser();
cout << "Enter matrix 2:" << endl;
matrix2.readFromUser();
cout << "Matrix 1:" << endl;
matrix1.print();
cout << "Matrix 2:" << endl;
matrix2.print();
Matrix3x3 matrixSum = matrix1 + matrix2;
cout << "Sum of Matrix 1 and Matrix 2:" << endl;
matrixSum.print();
Matrix3x3 matrixProduct = matrix1 * matrix2;
cout << "Product of Matrix 1 and Matrix 2:" << endl;
matrixProduct.print();
cout << "Number of Matrix3x3 objects created: " << Matrix3x3::getObjectCount() << endl;
return 0;
}
OUTPUT:
Enter matrix 1:
Enter the elements of the 3x3 matrix (row-wise):
1 2 3
4 5 6
3 6 7
Enter matrix 2:
Enter the elements of the 3x3 matrix (row-wise):
3 3 3
2 2 2
1 1 1
Matrix 1:
1 2 3
4 5 6
3 6 7
Matrix 2:
3 3 3
2 2 2
1 1 1
Sum of Matrix 1 and Matrix 2:
4 5 6
6 7 8
4 7 8
Product of Matrix 1 and Matrix 2:
10 10 10
28 28 28
28 28 28
Number of Matrix3x3 objects created: 4
Explanation
Matrix Class:
- The
readFromUser
method is added to read matrix elements from the user. - The
print
method displays the matrix elements. - Other class members and methods remain unchanged.
- The
Main Function:
- Two
Matrix3x3
objects (matrix1
andmatrix2
) are created using the default constructor. - The
readFromUser
method is called to read the elements of each matrix from the user. - The matrices are displayed using the
print
method. - Addition and multiplication of the matrices are demonstrated using the overloaded operators.
- The count of matrix objects is displayed using the static member function
getObjectCount
.
- Two
No comments:
Post a Comment