ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 8부 (클래스) 종합문제 - 1
    C++ 2021. 12. 23. 18:06

    https://www.youtube.com/watch?v=A5QdMcTe_Hg&list=PLlJhQXcLQBJqywc5dweQ75GBRubzPxhAk&index=68 

    문제1.

    //다음 프로그램의 출력 결과는?

    #include <iostream>
    
    using namespace std;
    
    int n = 0;
    namespace A {
    	int n = 0;
    	namespace B {
    		void set() { n = 10; }
    		int n = 0;
    	}
    }
    
    namespace C {
    	void set();
    	int n = 0;
    }
    
    void C::set() { n = 20; }
    
    int main() {
    	using namespace A::B;
    
    	set();
    	C::set();
    
    	cout << ::n << endl;
    	cout << A::n << endl;
    	cout << A::B::n << endl;
    	cout << C::n << endl; 
    }

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    정답1.

    0

    10

    0

    20

     

     

     

    문제2.

    /*
    아래의 GameWindow 클래스의 코드에서 창 너비와 높이를 매개변수로 받는 ResizeWindow 함수를 추가하여
    다음 조건과 같이 width와 height의 값을 바꿀 수 있도록 하세요.
    - 들어온 매개변수의 값과 일치하도록 창 크기를 설정할 것.
    - 너비가 800보다 작거나 높이가 600보다 작을 때는 각각 800, 600으로 설정할 것

    2. GameWindow::GameWindow(int w, int h) 생성자도 마찬가지로 2번의 조건을 만족하도록 수정하세요.

    3. 작성한 코드에서 상수화가 가능한 부분을 모두 상수화하세요.
    */

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    정답2.

    #include <iostream>
    
    #include <iostream>
    
    using namespace std;
    
    class GameWindow {
    public:
    	GameWindow();
    	GameWindow(const int,const int);
    
    	int GetWidth() const;
    	int GetHeight() const;
    
    	void ResizeWindow(const int, const int);
    
    private:
    	int width;
    	int height;
    };
    
    GameWindow::GameWindow() : width(800), height(600) { }
    GameWindow::GameWindow(const int w,const int h) {
    	ResizeWindow(w, h);
    }
    void GameWindow::ResizeWindow(const int w, const int h) {
    	if (w < 800) width = 800;
    	else width = w;
    
    	if (h < 600) height = 600;
    	else height = h;
    }
    int GameWindow::GetWidth() const { return width; }
    int GameWindow::GetHeight() const { return height; }
    
    int main() {
    	GameWindow mainWindow;
    	mainWindow.ResizeWindow(1366, 768);
    	cout << mainWindow.GetWidth() << "x" << mainWindow.GetHeight() << endl;
    }

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

    동적 할당  (0) 2021.12.23
    8부 (클래스) 종합문제 - 2  (0) 2021.12.23
    연산자 오버로딩  (0) 2021.12.23
    멤버 메서드 활용하기  (0) 2021.12.23
    상수형 매개변수와 상수형 메서드  (0) 2021.12.23
Designed by Tistory.