250x250
Notice
Recent Posts
«   2024/07   »
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
관리 메뉴

일상 코딩

[C++/8.2] 캡슐화, 접근 지정자, 접근함수 본문

C++/따배C++ 08강 객체지향 기초

[C++/8.2] 캡슐화, 접근 지정자, 접근함수

polarcompass 2021. 10. 20. 03:14
728x90
#include<iostream>
#include<vector>
#include<string>

using namespace std;

class Date
{
// 기본이 private으로 설정되어있음. 안적어도 무방함.
private:  // access specifier
    int m_month;
    int m_day;
    int m_year;

public:
    void setDate(const int& day_input, const int& year_input)
    {
        m_day = day_input;
        m_year = year_input;
    }

    void setMonth(const int& month_input)
    {
        m_month = month_input;
    }

    const int& getDay() // const로 수정을 막아준다.
    {
        return m_day;
    }

    void copyFrom(const Date& original)
    {
        m_month = original.m_month;
        m_day = original.m_day;
        m_year = original.m_year;
    }
};

int main()
{
    Date today; //{8,4,2025};
    today.setDate(4,2025);
    today.setMonth(8);

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

    // 같은 class에서 나온 다른 instance끼리는 서로 접근이 가능하다.
    Date copy;
    copy.copyFrom(today);    
    
    // ..

    return 0;
}
728x90