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