Skip to content

Commit d6e2960

Browse files
committedJul 5, 2020
+, - and * operators overloading in a simple Matrix class example ..
1 parent 97c8a62 commit d6e2960

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
 

‎operators/op_overloading.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ class Matrix {
3333
}
3434
return tmp;
3535
}
36+
// Overload * Operator for Objects of type class --> Matrix
37+
//
38+
Matrix operator*(int multiplier) {
39+
int counter{0};
40+
Matrix tmp(*this);
41+
while(counter != vec.size()) {
42+
tmp.vec.at(counter) *= multiplier;
43+
counter++;
44+
}
45+
return tmp;
46+
}
3647
void print() {
3748
for(int i=0; i<vec.size(); i++) {
3849
cout << vec.at(i) << " ";
@@ -73,5 +84,9 @@ int main()
7384
Matrix m4 = m3 + m3;
7485
m4.print();
7586

87+
cout << "m5 = m4*2 = ";
88+
Matrix m5 = m4*2;
89+
m5.print();
90+
7691
return 0;
7792
}

0 commit comments

Comments
 (0)
Please sign in to comment.