일상 코딩
[C++/11.01] 상속 기본 1 본문
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 |