티스토리 뷰

190621(4일차-정리end) - 연산자 오버로딩

교육을 받으면서 노트필기 했던 내용을 날것 그대로 업로드합니다.


 
  • 연산자 오버로딩
 
ob1 + ob2
ob1.operator+(ob2) 로 수행된다.
 
#include <iostream>
using namespace std;
 
class pos {
    int x;
    int y;
public:
    pos(int x = 10, int y = 20) :x(x), y(y) {}
    pos operator+(const pos& rf) {//
        pos tmp;
        tmp.x = x + rf.x;
        tmp.y = y + rf.y;
        return tmp;
    }
    void print() {
        cout << x << ", " << y << endl;
    }
};
 
int main() {
    pos p1;
    pos p2;
    pos p3 = p1 + p2;
    p3.print();
}
20, 40
 
  • ostream : 
cout<<obj 는
cout.operator<<(obj); (?) 예전 설계한 것에 이후 설계한 것을 대입한다? X
전역함수로 만들어야한다. operator<<(cout,obj) 전역함수 콜로 실행해야 하기 때문이다.
 
#include <iostream>
using namespace std;
 
 
class pos {
    int x;
    int y;
public:
    pos(int x = 10, int y = 20) :x(x), y(y) {}
    pos operator+(const pos& rf) {
        pos tmp;
        tmp.x = x + rf.x;
        tmp.y = y + rf.y;
        return tmp;
    }
 
    void print() {
        cout << x << ", " << y << endl;
    }
 
    // private 멤버에 접근하기위해 friend로 지정한다.
    friend ostream& operator<<(ostream &os, pos &ps);
 
 
};
 
//객체1.operator<<(객체2)의 형태로의 호출은 불가하다.
//전역함수로 만든 이유는 객체1(cout)이 객체2를 모르기 때문이다.
ostream& operator<<(ostream &os, pos &ps) {
    os << ps.x << "," << ps.y << endl;
    return os;
}
 
int main() {
    pos p1;
    pos p2;
    pos p3 = p1 + p2;
    //p3.print();
    cout << p3;
}
20,40
 
  • 전역함수로 -연산자 오버로딩 해봅시다.
class pos {
    int x;
    int y;
public:
    pos(int x = 10, int y = 20) :x(x), y(y) {}
    pos operator+(const pos& rf) {
        pos tmp;
        tmp.x = x + rf.x;
        tmp.y = y + rf.y;
        return tmp;
    }
    /*pos operator-(const pos& rf) {
        pos tmp;
        tmp.x = x - rf.x;
        tmp.y = y - rf.y;
        return tmp;
    }
*/
    void print() {
        cout << x << ", " << y << endl;
    }
 
    // private 멤버에 접근하기위해 friend로 지정
    friend ostream& operator<<(ostream &os, pos &ps);
    friend pos operator-(const pos& rf1, const pos& rf);
 
};
 
//객체1.operator<<(객체2)의 형태로의 호출은 불가
//전역함수로 만든 이유는 객체1(cout)이 객체2를 모르기 때문이다.
ostream& operator<<(ostream &os, pos &ps) {
    os << ps.x << "," << ps.y << endl;
    return os;
}
 
pos operator-(const pos& rf1,const pos& rf2) {
    pos tmp;
    tmp.x = rf1.x - rf2.x;
    tmp.y = rf1.y - rf2.y;
    return tmp;
}
 
//두 객체의 차를 해주는 operator- 함수를 만드시오
//멤버함수버전, friend 전역함수버전
 
int main() {
    pos p1(100,100);
    pos p2;
    pos p3 = p1 - p2;
    //p3.print();
    cout << p3;
}
 
10+obj 인경우
10.operator+(obj) 인데 10은 상수라 불가능하다.
때문에 전역함수를 따로 정의하여 사용해야 되는 것입니다.
 
friend pos operator+(const int num, const pos& rf2);
.
.
.
//5+ob4 를 위한 operator+ 함수를 오버로딩
pos operator+(const int num, const pos& rf2) {
    pos tmp;
    tmp.x = num + rf2.x;
    tmp.y = num + rf2.y;
    return tmp;
}
 
 
  • ++후위인 경우 int 인자가 한 개 더있다고 가정한다.
    const pos operator++(int) {
        pos tmp;
        tmp.x = y;
        tmp.y = y;
        x++;
        y++;
        return tmp;
    }
 
  • []연산자 오버로딩
    obj[2] 는
    obj.operator[](2)
 
#include <iostream>
using namespace std;
 
class pos {
    int x;
    int y;
public:
    pos(int x = 10, int y = 20) :x(x), y(y) {}
    friend ostream& operator<<(ostream &os, pos &ps);
};
ostream& operator<<(ostream &os, pos &ps) {
    os << ps.x << "," << ps.y << endl;
    return os;
}
class pos_array {
    pos parr[10];
    int cnt;
public:
    pos& operator[](int n) {
        return parr[n];
    }
};
int main() {
    pos_array ob1;
    ob1[0] = pos(1, 2);
    ob1[1] = pos(2,3 );
    cout << ob1[0];
    cout << ob1[1];
}
1,2
2,3
 
  • 배열[] 연산자에서 const를 추가해주면 오버로딩 역할을 해준다. const없을 시 대입할 때 사용되고 const있을 시 출력 등에 사용된다.
 
  • 복사생성자
#include <iostream>
using namespace std;
 
class AA {
public:
    AA() {}
 
    //기본 복사생성자
    AA& operator=(const AA& rf) {
        cout << "AA" << endl;
        return *this;
    }
};
class BB :AA {
public:
    BB() {} // 파생에서 대입연산자 오버라이딩시 기초(부모)클래스 대입연산자
    //명시적인 호출이 필요하다.
 
    //기본 복사생성자
    BB& operator=(const BB& rf) {
        AA::operator=(rf); // 원래 이것도 자동으로 실행된다.
        //명시적인 호출이 필요하다.
        cout << "BB" << endl;
        return *this;
    }
};
int main() {
    BB ob1;
    BB ob2;
    ob2 = ob1;
}
 
  • x->func(); 은
    x(.operator->())->func(); 라고보면된다.
 
#include <iostream>
using namespace std;
 
 
class pos {
    int x;
    int y;
public:
    pos(int x = 10, int y = 20) :x(x), y(y) {}
    friend ostream& operator<<(ostream &os, pos &ps);
};
ostream& operator<<(ostream &os, pos &ps) {
    os << ps.x << "," << ps.y << endl;
    return os;
}
class pos_array {
    pos parr[10];
    int cnt;
public:
    pos& operator[](int n) {
        return parr[n];
    }
};
class pointer {
    pos aa;
public:
    pos& operator*() {
        cout << "*" << endl;
        return aa;
    }
    /*pos& operator->() {
        cout << "->" << endl;
        return aa;
    }*/
    void print() {
        cout << aa << endl;
    }
};
int main() {
    
    pointer ob1;
    pointer * p = &ob1;
    //p->print();
    cout << *ob1 << endl;
    
}
 
  • 형변환 연산자 operator int(){return (int)x;};
    operator double() {return re;};
 
  • 사용 금지하기 위해 하는 연산자 오버로딩
class AA {
private:
    void operator=(const AA&);
    void operator&();
    void operator,(const AA&);
};
 
  • 템플릿
template <typename T>
T add(T a, T b) {
 
}
 
#include <iostream>
using namespace std;
 
template <typename T>
T add(T a, T b) {
 
}
void main() {
    add<int>(10, 20);
    add<int>(11, 20);
    add(3.4,5.5);
}
 
  • 예외처리
  • 스트림
  • STL
    • 컨테이너
    • 반복자
    • 어뎁터
    • 알고리즘
 
 
 
댓글
최근에 올라온 글
최근에 달린 댓글
네이버 이웃추가
«   2024/04   »
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
글 보관함