• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

(11:40)class안의 함수 접근

22.08.15 09:44 작성 조회수 87

0

#include <iostream>
#include<string>
#include<vector>

using namespace std;

class Date {
    int _month;
    int _day;
    int _year;

public:
    void setDate(const int& month_input, const int& day_input, const int& year_input) {
        _month = month_input;
        _day = day_input;
        _year = year_input;
    }
    int getDay() {
        return _day;
    }

};

int main() {

    Date today;
    today.setDate(8, 15, 2022);

    cout<<today.getDay()<<endl;

    return 0;
}

setDate()는 public으로 지정한 다음 main()에서 접근을 했는데,

getDay()함수는 아무 접근 지정자도 표시를 안 했는데 바로 main()에서 접근할 수 있는 이유가 무엇인가요?

따로 지정하지 않으면 private이 된다고 했는데, getDay()는 바로 사용할 수 있는 이유가 궁금합니다.

답변 1

답변을 작성해보세요.

2

강민철님의 프로필

강민철

2022.08.16

안녕하세요,

public: 을 통해 지정되는 함수/변수의 범위에 혼동을 하신 것 같습니다.

첨부해주신 코드 상의 getDay 또한 public 입니다.

public: 하의 함수/변수들은 모두 public입니다.

 

아래와 같이요.

 

class DateClass
{
public:
    int m_month; // public
    int m_day;   // public
    int m_year;  // public
};

 

예제 출처 https://boycoding.tistory.com/242