https://www.learncpp.com/cpp-tutorial/public-vs-private-access-specifiers/
public member는 누구나 접근할 수 있다
struct는 public default이고
class는 private default라는 차이점이 있다
private member는 외부에서 접근이 불가능하다
오직 class의 다른 member로부터만 접근이 가능하다
따라서 이러한 private에 접근하기 위해
public member를 이용하도록 interface를 구성하고는 한다
#include <iostream>
class DateClass // members are private by default
{
int m_month {}; // private by default, can only be accessed by other members
int m_day {}; // private by default, can only be accessed by other members
int m_year {}; // private by default, can only be accessed by other members
public:
void setDate(int month, int day, int year) // public, can be accessed by anyone
{
// setDate() can access the private members of the class because it is a member of the class itself
m_month = month;
m_day = day;
m_year = year;
}
void print() // public, can be accessed by anyone
{
std::cout << m_month << '/' << m_day << '/' << m_year;
}
};
int main()
{
DateClass date;
date.setDate(10, 14, 2020); // okay, because setDate() is public
date.print(); // okay, because print() is public
std::cout << '\n';
return 0;
}
우리가 m_mont와 m_day m_year에 직접 접근할 수는 없지만
setData는 public member function으로 우회적으로 정보를 수정할 수 있도록
interface가 구성되어 있다