블로그 이미지
송시혁

calendar

1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Notice

Tag

Recent Post

Recent Comment

Recent Trackback

Archive

2013. 11. 6. 23:22 c++


#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;
}

실행결과




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

11월 7일 생성자와 소멸자 순서  (0) 2013.11.07
11월 6일 생성자와 소멸자  (0) 2013.11.06
11월 6일 class 내부 함수  (0) 2013.11.06
11월 4일 함수와 레퍼런스(정리 중)  (0) 2013.11.04
11월 4일 동적할당  (0) 2013.11.04
posted by 송시혁