ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C++ 연습문제 9.8
    C++ 2021. 11. 7. 16:12
    728x90
    반응형

    Point 클래스에 전위 감소 연산자와 후위 감소 연산자를 추가하고 이를 테스트해보라.

     

    내 코드(main.cpp) : 

    #include <iostream>
    #include "point.h"
    using namespace std;
    
    int main() {
    	Point pt1(1, 1);
    	Point pt2 = pt1++;
    	Point pt3 = ++(++pt1);
    	//Point pt4 = (pt1++)++;
    	Point pt5 = (++pt1)++;
    
    	pt1.Print();
    	pt2.Print();
    	pt3.Print();
    	//pt4.Print();
    	pt5.Print();
    
    	return 0;
    }

     

    point.h :

    #include <iostream>
    using namespace std;
    
    class Point {
    private:
    	int x_, y_;
    
    public:
    	Point(int x = 0, int y = 0) : x_(x), y_(y) {}
    
    	friend Point& operator++(Point& pt);
    	friend Point operator++(Point& pt, int);
    	
    	void Print() {
    		cout << "(" << x_ << ", " << y_ << ")" << endl;
    	}
    };
    
    inline Point& operator++(Point& pt) {
    	pt.x_++;
    	pt.y_++;
    	return pt;
    }
    
    inline Point operator++(Point& pt, int) {
    	Point temp = pt;
    	pt.x_++;
    	pt.y_++;
    	return temp;
    }
    728x90
    반응형

    'C++' 카테고리의 다른 글

    C++ 연습문제 11.6  (0) 2021.11.20
    C++ 연습문제 9.11  (0) 2021.11.07
    C++ 연습문제 9.2  (0) 2021.10.30
    C++ 연습문제 8.5  (0) 2021.10.30
    C++ 연습문제 7.9  (0) 2021.10.19
Designed by Tistory.