8.2 캡슐화, 접근 지정자, 접근 함수
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Date
{
// private이 기본.
int m_month;
int m_day;
int m_year;
public: // access specifier
void setDate(const int &month_input,const int& day_input, const int& year_input)
{
m_month = month_input; // 이런식으로 private에 접근 가능
m_day = day_input;
m_year = year_input;
}
void setMonth(const int& month_input) // setters
{
m_month = month_input;
}
const int getDay() // getters -> 변수명을 바꿀 때에도 좋다
{
return m_day;
}
void copyFrom(const Date& original)
{
m_month = original.m_month; // 다른 instnace의 private에 접근 가능
m_day = original.m_day;
m_year = original.m_input;
}
};
int main()
{
Date today;
//today.m_moth = 8;
//today.m_day = 4;
//today.m_year = 2025;
today.setDate(8,4,2025);
//cout << today.getDay() << "\n";
Date copy;
//copy.setDate(today.getMonth(), today.getDay(), ...); // 귀찮다.
copy.copyFrom(today); //이렇게 instance를 인수로 받을 수 있으면 편할 것.
//
return 0;
}
private에 있는 데이터에 접근하기 위해서는 access function이라는 것을 통해 접근해야한다.
class 안에 public의 access function을 통해 접근 가능.