-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathp025.cpp
More file actions
163 lines (143 loc) · 2.94 KB
/
p025.cpp
File metadata and controls
163 lines (143 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//WAP to show the sum of matrix using operator overloading as a member function as well as subtraction , multiplication of operator overloading using friend function
#include <iostream>
using namespace std;
class matrix
{
private:
int a[2][2], sum[2][2], i, j;
public:
void input_matrix()
{
cout << "\nEnter Matrix Element \n";
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
cin >> a[i][j];
}
}
}
void display()
{
cout << "Matrix elements are " << endl;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
cout << a[i][j] << ' ';
}
cout << endl;
}
cout << endl;
}
void operator+(matrix obj1)
{
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
sum[i][j] = a[i][j] + obj1.a[i][j];
}
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
// Print the sum of matrix
cout << sum[i][j] << ' ';
}
cout << endl;
}
}
friend void operator-(matrix, matrix);
friend void operator*(matrix, matrix);
};
void operator-(matrix obj1, matrix obj2)
{
int i, j, sub[2][2];
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
sub[i][j] = obj1.a[i][j] - obj2.a[i][j];
}
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
// Print the difference of matrix
cout << sub[i][j] << ' ';
}
cout << endl;
}
}
void operator*(matrix obj1, matrix obj2)
{
int i, j, mul[2][2];
cout << "MULTIPLICATION of matrix is :" << endl;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
mul[i][j] = 0;
for (int k = 0; k < 2; k++)
{
mul[i][j] = mul[i][j] + (obj1.a[i][j] * obj2.a[i][j]);
}
}
cout << "\n";
}
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
cout << mul[i][j] << " ";
}
cout << "\n";
}
}
int main()
{
matrix m1, m2;
m1.input_matrix();
m1.display();
m2.input_matrix();
m2.display();
cout << "Sum of matrix is " << endl;
cout << endl;
m1 + m2;
cout << "Difference of matrix is " << endl;
cout << endl;
m1 - m2;
m1 *m2;
return 0;
}
/*
OUTPUT:
Enter Matrix Element
1
2
3
4
Matrix elements are
1 2
3 4
Enter Matrix Element
7
8
9
6
Matrix elements are
7 8
9 6
Sum of matrix is
8 10
12 10
Difference of matrix is
-6 -6
-6 -2
MULTIPLICATION of matrix is :
14 32
54 48
*/