blob: 200f4d8392d3e7719fac92c5821afd8913fbf282 (
plain) (
blame)
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
|
#include "Exercise6.h"
#include <iostream>
/* Private */
void Matrix::addBy(int amount) {
for (int i = 0; i < 4; i++) {
values[i] += amount;
}
}
/* Public */
Matrix::Matrix(int a, int b, int c, int d) {
values[0] = a;
values[1] = b;
values[2] = c;
values[3] = d;
}
Matrix& Matrix::operator++() { // ++matrix
addBy(+1);
return *this;
}
Matrix Matrix::operator++(int) { // matrix++
Matrix copy = *this;
addBy(+1);
return copy;
}
Matrix& Matrix::operator--() { // --matrix
addBy(-1);
return *this;
}
Matrix Matrix::operator--(int) { // matrix--
Matrix copy = *this;
addBy(-1);
return copy;
}
Matrix& Matrix::operator+=(const Matrix& rhs) {
for (int i = 0; i < 4; i++) {
values[i] += rhs.values[i];
}
return *this;
}
Matrix& Matrix::operator-=(const Matrix& rhs) {
for (int i = 0; i < 4; i++) {
values[i] -= rhs.values[i];
}
return *this;
}
Matrix& Matrix::operator*=(const Matrix& rhs) {
values[0] = values[0] * rhs.values[0] + values[1] * rhs.values[2];
values[1] = values[0] * rhs.values[1] + values[1] * rhs.values[3];
values[2] = values[2] * rhs.values[0] + values[3] * rhs.values[2];
values[3] = values[2] * rhs.values[1] + values[3] * rhs.values[3];
return *this;
}
/* Friend */
Matrix operator+(const Matrix& lhs, const Matrix& rhs) {
Matrix ret = lhs;
ret += rhs;
return ret;
}
Matrix operator-(const Matrix& lhs, const Matrix& rhs) {
Matrix ret = lhs;
ret -= rhs;
return ret;
}
Matrix operator*(const Matrix& lhs, const Matrix& rhs) {
Matrix ret = lhs;
ret *= rhs;
return ret;
}
std::ostream& operator<<(std::ostream& lhs, const Matrix& rhs) {
return lhs << rhs.values[0] << "," << rhs.values[1] << std::endl << rhs.values[2] << "," << rhs.values[3] << std::endl;
}
std::istream& operator>>(std::istream& lhs, Matrix& rhs) {
return lhs >> rhs.values[0] >> rhs.values[1] >> rhs.values[2] >> rhs.values[3];
}
|