작은 걸음 II — 만들며 배우기

객체에게 정보 물어보기

객체에게 정보 물어보기

이번에 배울 것

객체의 점 뒤에 함수 이름을 쓰면 그 객체에 속한 기능을 사용합니다. 이런 함수를 멤버 함수라고 합니다.

실행해 보기

예제 · example2.cpp
void small_main()
{
    Window window;

    window.set_title("Small Window");
    window.open(400, 300);

    print("Size: ", window.width(), " x ", window.height());

    while (window.is_open())
    {
        window.show();
    }
}

window.width()와 window.height()는 창의 가로·세로 크기를 돌려줍니다. 콘솔에 Size: 400 x 300이 나오는지 보세요.

객체 만들기와 실제 창 열기는 별개입니다. set_title은 open 전에 해도 됩니다. 프로그램에서 직접 닫으려면 close를 호출합니다.

Exercise — 크기 확인하기

320 x 240 창을 열고 실제 width와 height를 콘솔에 출력한 뒤 창을 유지하세요.

연습 문제 코드 · exercise2_starter.cpp
void small_main()
{
    Window window;
    window.open(320, 240);

    // print the size, then keep the window alive.
}

Hint

window.width()와 window.height()를 print에 전달하세요.

풀이 보기
void small_main()
{
    Window window;
    window.open(320, 240);

    print(window.width(), " x ", window.height());

    while (window.is_open())
        window.show();
}
← 이전 레슨다음 레슨 →