Notice
Recent Posts
Recent Comments
Link
오븐 노트
[C++] 상속성 본문
#include <iostream>
using namespace std;
// 객체지향 (OOP Object Oriented Programming)
// - 상속성
// - 은닉성
// - 다형성
// [ [ Player ] ]
// [ Knight ]
// 생성자(N)/소멸자(1)
// Player 생성자? Knight 생성자? -> 둘다 호출
// 중요! 순서는 부모생성자 -> 자식생성자 -> 자식소멸자 -> 부모소멸자
// class는 일종의 설계도
// RPG 게임 상속 구조
// GameObject
// - Creature
// -- Player, Monster, NPC, Pet
// - Projectile
// -- Arrow, Fireball
// - Env
// Item
// - Weapon
// -- Sword, Bow
// - Armor
// -- Helmet, Boots, Armor
// - Consumable
// -- Potion, Scroll
class GameObject
{
public:
int _objectld;
};
class Player : public GameObject
{
public:
Player()
{
_hp = 0;
_attack = 0;
_defence = 0;
cout << "Player() 기본 생성자 호출" << endl;
}
Player(int hp)
{
_hp = 0;
_attack = 0;
_defence = 0;
cout << "Player(int hp) 기본 생성자 호출" << endl;
}
~Player()
{
cout << "Player() 소멸자 호출" << endl;
}
void Move() { cout << "Player Move" << endl; }
void Attack() { cout << "Player Attack" << endl; }
void Die() { cout << "Player Die" << endl; }
public:
int _hp;
int _attack;
int _defence;
};
class Knight : public Player // public 상속
{
public:
Knight()
{
/*
선처리 영역
- 여기서 Player() 생성자 호출
*/
_stamina = 100;
cout << "Knight() 기본 생성자 호출" << endl;
}
Knight(int stamina) : Player(100)
{
/*
선처리 영역
- 여기서 Player(int hp) 생성자 호출
*/
_stamina = stamina;
cout << "Knight(int stamina) 기본 생성자 호출" << endl;
}
~Knight()
{
cout << "Knight() 소멸자 호출" << endl;
}
/*
후처리 영역
- 여기서 Player() 소멸자 호출
*/
// 재정의 (덮어쓰기. 다만, 구조적으로 좋은 구조는 아니기에 거의 사용하지 않는다. 재정의 이전 함수 호출 가능)
void Move() { cout << "Knight Move" << endl; }
public:
int _stamina;
};
class Mage : public Player
{
public:
public:
int _mana;
};
int main()
{
Knight k(100);
k._hp = 100;
k._attack = 10;
k._defence = 5;
k._stamina = 50;
k.Move(); // 재정의 함수 (다만, 구조적으로 좋은 구조는 아니기에 거의 사용하지 않는다.)
k.Player::Move(); // 재정의하기 이전 부모 클래스에 있는 함수
k.Attack();
k.Die();
return 0;
}
[C++과 언리얼로 만드는 MMORPG 게임 개발 시리즈] Part1: C++ 프로그래밍 입문 - 인프런 | 강의
C++ 카테고리의 글은 인프런 Rookiss님의 강의를 공부하며 정리하는 내용입니다.
이미 알고 있는 내용도 다시 정리 되어있을 수 있습니다.
모든 글은 제가 공부하기 위해 작성합니다.
'Develop > C++' 카테고리의 다른 글
[C++] 다형성 #1 ~ 2 (0) | 2022.09.28 |
---|---|
[C++] 은닉성 (0) | 2022.09.21 |
[C++] 생성자와 소멸자 #1 ~ 2 (0) | 2022.09.07 |
[C++] 객체지향의 시작 (0) | 2022.09.05 |
[C++] 파일 분할 관리 (0) | 2022.08.29 |