Notice
Recent Posts
250x250
«   2025/05   »
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++/11.01] 상속 기본 1 본문

C++/따배C++ 11강 상속

[C++/11.01] 상속 기본 1

polarcompass 2021. 11. 6. 19:18
728x90
#include<iostream>
using namespace std;

class Mother
{
private:
    int m_i;

public:
    Mother(const int & i_in = 0)
        : m_i(i_in)
    {
        cout << "Mother constructor" << endl;
    }

    void setValue(const int& i_in)
    {
        m_i = i_in;
    }

    int getValue()
    {
        return m_i;
    }
};

class Child : public Mother // derived class
{
private:
    double m_d;

public:
    Child(const int & i_in, const double & d_in)
        : Mother(i_in)
        , m_d(d_in)
    {}

    void setValue(const int & i_in, const double & d_in)
    {
        Mother::setValue(i_in);
        m_d = d_in;
    }

    double getValue()
    {
        return m_d;
    }
};

class Daughter : public Mother {};

class Son : public Mother {};

int main()
{
    Mother mother;
    mother.setValue(1024);
    cout << mother.getValue() << endl;

    Child child(1024,128);
    child.Mother::setValue(128);
    cout << child.getValue() << endl;

    return 0;
}
728x90

'C++ > 따배C++ 11강 상속' 카테고리의 다른 글

[C++/11.03] 유도된 클래스들의 생성 순서  (0) 2021.11.07
[C++/11.02] 상속 기본 2  (0) 2021.11.06