-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_002.cpp
More file actions
74 lines (68 loc) · 1.22 KB
/
problem_002.cpp
File metadata and controls
74 lines (68 loc) · 1.22 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
//Write a C++ program to illustreate the operator overloading concept using matrix addition
//as an example.
#include<iostream>
using namespace std;
class Matrix{
int row,col;
public:
int** arr;
Matrix(int r,int cl);
void getValue();
void print();
Matrix operator+(Matrix& m2);
};
Matrix::Matrix(int r,int cl)
{
row=r;
col=cl;
arr = new int*[row];
for(int i=0;i<row;i++)
{
arr[i]=new int[col];
}
}
void Matrix::getValue()
{
for(int j=0;j<row;j++){
for(int k=0;k<col;k++)
{
cout<<"Enter value :";
cin>>arr[j][k];
}
}
}
void Matrix::print()
{
for(int j=0;j<row;j++){
for(int k=0;k<col;k++)
{
cout<<arr[j][k]<<" ";
}
cout<<endl;
}
}
Matrix Matrix::operator+(Matrix& m2)
{
Matrix m(row,col);
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
m.arr[i][j]= arr[i][j]+m2.arr[i][j];
}
}
return m;
}
int main()
{
Matrix m1(3,3);
m1.getValue();
m1.print();
Matrix m2(3,3);
m2.getValue();
m2.print();
Matrix sum(3,3);
sum=m1+m2;
cout<<"Sum of Matrix:\n";
sum.print();
}