#include <iostream>
using namespace std;
// 맴버로 구현
class Matrix {
int a, b, c, d;
public:
Matrix(int a=0, int b=0, int c=0, int d=0) {
this->a = a;
this->b = b;
this->c = c;
this->d = d;
}
void show() { cout << "Matrix = {" << this->a << ',' << this->b << ','<<this->c << ',' << this->d << "}"<< endl; }
Matrix operator+(Matrix op2);
Matrix& operator+=(Matrix op2);
bool operator==(Matrix op2);
};
Matrix Matrix::operator+(Matrix op2) {
Matrix tmp;
tmp.a = a + op2.a;
tmp.b = b + op2.b;
tmp.c = c + op2.c;
tmp.d = d + op2.d;
return tmp;
}
Matrix& Matrix::operator+=(Matrix op2) {
a = a + op2.a;
b = b + op2.b;
c = c + op2.c;
d = d + op2.d;
return *this;
}
bool Matrix::operator==(Matrix op2) {
if (a == op2.a && b == op2.b && c == op2.c) return true;
else return false;
}
int main() {
Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
c = a + b;
a += b;
a.show(); b.show(); c.show();
if (a == c)
cout << "a and c are the same" << endl;
}
#include <iostream>
using namespace std;
// friend 로 구현
class Matrix {
int a, b, c, d;
public:
Matrix(int a=0, int b=0, int c=0, int d=0) {
this->a = a;
this->b = b;
this->c = c;
this->d = d;
}
void show() { cout << "Matrix = {" << this->a << ',' << this->b << ','<<this->c << ',' << this->d << "}"<< endl; }
friend Matrix operator+(Matrix op1, Matrix op2);
friend Matrix& operator+=(Matrix& op1,Matrix op2);
friend bool operator==(Matrix op1, Matrix op2);
};
Matrix operator+(Matrix op1, Matrix op2) {
Matrix tmp;
tmp.a = op1.a + op2.a;
tmp.b = op1.b + op2.b;
tmp.c = op1.c + op2.c;
tmp.d = op1.d + op2.d;
return tmp;
}
Matrix& operator+=(Matrix& op1,Matrix op2) {
op1.a = op1.a + op2.a;
op1.b = op1.b + op2.b;
op1.c = op1.c + op2.c;
op1.d = op1.d + op2.d;
return op1;
}
bool operator==(Matrix op1,Matrix op2) {
if (op1.a == op2.a && op1.b == op2.b && op1.c == op2.c) return true;
else return false;
}
int main() {
Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
c = a + b;
a += b;
a.show(); b.show(); c.show();
if (a == c)
cout << "a and c are the same" << endl;
}
Colored by Color Scripter
|