Native/C++

const member function

aucd29 2013. 10. 2. 18:58
[code]#include <turboc.h>

class position
{
private:
    int x, y;
    char ch;

public:
    position(int ax, int ay, char ach)
    {
        x = ax;
        y = ay;
        ch = ach;
    }

    // 어떤 멤버 한수가 값을 읽기만 하고 바꾸지는 않는다면
    // const를 붙이는 것이 원칙이며 이 원칙대로 클래스를
    // 생성하여야 한다.
    void outPosition() const
    {
        gotoxy(x, y);
        putch(ch);
    }

    void moveTo(int ax, int ay)
    {
        x = ax;
        y = ay;
    }
};

void main()
{
    position here(1, 2, 'a');
    here.moveTo(20,5);
    here.outPosition();

    /*const position there(3, 4, 'b');
    there.moveTo(40, 10);        // error
    there.outPosition();*/
}
[/code]