c++

11월 6일 class의 선언부와 구현부

송시혁 2013. 11. 6. 23:22


#include <iostream>
using namespace std;

class IntSample
{
   public:
           void ShowScore();
           void setScore(const int s);
           int getScore();
   private://비공개 실행하지 못하게 한다. C++에서는 디폴트
           int Score;
};

// class intSample을 선언부(변수 선언, 함수 선언과 비슷한 개념)이라 하면,
//그 외부에 있는 함수는 구현부라고 부른다.
// 구현부
void IntSample::ShowScore()
{
   cout <<"점수: " << Score << endl;
}

void IntSample::setScore(const int s)
{
      if(s>100)
       {
         Score=100;
        return;
       }
      if(s<0)
      {
        Score=0;
       return;
      }
       Score=s;
}

int IntSample::getScore()
{
   return Score;// setScore()함수에서의 Score값을 리턴.
}

int main()
{
   IntSample obj;
  //obj.setScore(999); 주석 처리 되어있으나 999를 넣어도 100으로 출력된다.
  obj.setScore(-1);
  cout <<"점수: " <<obj.getScore() << endl;

  return 0;
}

실행결과