ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C++ 연습문제 7.4
    C++ 2021. 10. 19. 18:27
    728x90
    반응형

    다음 main 함수가 수행될 수 있도록 Point 클래스에 SetX와 SetY 멤버 함수를 추가해 보라.

    int main() {
    	Point pt1;
    
    	pt1.SetX(3).SetY(4);
    	pt1.Print();
    	
    	pt1.SetY(6).SetX(5);
    	pt1.Print();
    
    	return 0;
    }

     

    · 실행 결과

    (3, 4)
    (5, 6)

     

    내 코드(Point.h) : 

    #include <iostream>
    using namespace std;
    
    class Point {
    public:
    	Point& SetX(int x) {
    		x_ = x;
    		return (*this);
    	}
    
    	Point& SetY(int y) {
    		y_ = y;
    		return (*this);
    	}
    
    	void Print() {
    		cout << "(" << x_ << ", " << y_ << ")" << endl;
    	}
    
    private:
    	int x_, y_;
    };

     

    main 코드(main.cpp) :

    #include "Point.h"
    using namespace std;
    
    int main() {
    	Point pt1;
    
    	pt1.SetX(3).SetY(4);
    	pt1.Print();
    	
    	pt1.SetY(6).SetX(5);
    	pt1.Print();
    
    	return 0;
    }
    728x90
    반응형

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

    C++ 연습문제 8.5  (0) 2021.10.30
    C++ 연습문제 7.9  (0) 2021.10.19
    C++ 연습문제 6.4  (0) 2021.10.19
    C++ 연습문제 2.23  (0) 2021.10.19
    C++ 연습문제 2.22  (0) 2021.10.19
Designed by Tistory.