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 |
#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; }
|
cs |
'C++' 카테고리의 다른 글
명품 c++ 7장 실습문제 9번 (0) | 2019.06.06 |
---|---|
명품 c++ 7장 실습문제 8번 (0) | 2019.06.06 |
명품 c++ 7장 실습문제 5번 (0) | 2019.06.06 |
명품 c++ 7장 실습문제 4번 (0) | 2019.06.06 |
명품 c++ 7장 실습문제 3번 (0) | 2019.06.06 |