class Warrior
{
public:
void Move()
{
cout << "Move" << endl;
}
void SpecialMove()
{
cout << "specialMove" << endl;
}
public:
int _stamina;
int _hp;
int _datmage;
int _defence;
};
class Mage
{
public:
void Move()
{
cout << "Move" << endl;
}
public:
int _mp;
int _hp;
int _datmage;
int _defence;
};
int main()
{
Warrior w1;
w1.Move();
w1.SpecialMove();
}
상속후
class Player
{
public:
void Move()
{
cout << "Move" << endl;
}
public:
int _hp;
int _datmage;
int _defence;
};
class Warrior : public Player
{
public:
void SpecialMove()
{
cout << "specialMove" << endl;
}
public:
int _stamina;
};
class Mage : public Player
{
public:
int _mp;
};
int main()
{
Warrior w1;
w1.Move();
w1.SpecialMove();
}
차로 예를 들자면
public | protected, public |
---|---|
핸들조작, 문 여는법, 엑셀밟는 법 | 분해하는 법, 엔진 가동시키는법 |
그러면 protected와 private 차이점
protected는 상속 받은 자식들만 접근 가능
private는 오직 자신만
이렇게 보안적으로 필요한 요소에 해당 키워드들을 적절히 사용하여 보안을 강화
class Player
{
public:
virtual void Jump()
{
cout << "일반 점프" << endl;
}
public:
int _hp = 100;
int _damage = 10;
};
class Knight : public Player
{
public:
virtual void Jump() override
{
cout << "슈퍼점프" << endl;
}
};
void CompareJump(Player* p1, Player* p2)
{
p1->Jump();
p2->Jump();
}
void main()
{
Player p1;
Knight k1;
CompareJump(&p1, &k1);
}
아래는 해당 코드에 대한 컴퓨터가 내부적으로 어떻게 동작하는지에 대한 그림
추상 메서드를 만들어 줄 수 있음
특정 메서드의 구현을 강제하여 필수적 동작 누락을 방지
virtual void Move() = 0;
or
virtual void Move() abstract;