#include
using std::cout
;
using std::endl;
class A // 기본 클래스
{
int x;
public:
void setX
(int
i) {
x =
i;
}
void showX() { cout << x << endl; }
};
class
B
:public
A //파생 클래스
{
//아무 것도 없어요. 그러나
};
int main() {
A aa;
aa.setX(1);
aa.showX(); B bb;
bb.setX(10);
bb.showX();
return 0;
}
**public 으로 상속 하여 접근 가능
#include
using std::cout;
using std::endl;
class A
{
int x;
public:
void setX(int i) { x = i; }
void showX() { cout << x << endl; }
};
class B :public A
{
int y;
public:
void setY(int i) { y = i; }
void
showXY() { showX(); cout << y << endl; }
};
int main()
{
B bb;
bb.setX(1); // 기본클래스의 멤버접근
bb.setY(2); // 파생클래스의 멤버접근
bb.showX(); // 기본클래스의 멤버접근
bb.showXY(); // 파생클래스의 멤버접근
return 0;
}
#include
using std::cout;
using std::endl;
class A
{
int x;
public:
void setX(int i) { x = i; }
void showX() { cout << x << endl; }
};
class B :private A //비공개적으로 상속받는다
{
int y;
public:
void setXY(int i, int j) { setX(i);y = j; }
// 기본 클래스의 public 멤버 접근
void showXY() { showX();cout << y << endl; }
};
int main()
{
B bb;
bb.setXY(1, 2); // 파생클래스의 멤버접근
bb.showXY(); // 파생클래스의 멤버접근
return 0;
}
상속은 pulblic을 사용
생성자는 부모 먼저
소멸자는 자식 후 부모