회원 로그인
|
메소드 오버라이드
메소드 오버라이드
- 기반 클래스에서 상속 받은 메소드를 파생 클래스에서 재정의 해서 사용하는 것을 메소드 오버라이드라고 한다.
- 기반 클래스에서 상속 받은 메소드를 파생 클래스에서 재정의 해서 사용하는 것을 메소드 오버라이드라고 한다.
// 오버라이드 #include <iostream> using namespace std; class AAA { public: void print() { cout << "AAA" << endl; } }; class BBB : public AAA { public: void print() { // 오버라이드 cout << "BBB" << endl; } }; void main() { BBB b; b.print(); }
#include <iostream> #include <stdlib.h> /* srand, rand */ #include <time.h> /* time */ using namespace std; // 부모 클래스는 protected, public만 주로 사용 // C++ 다중 상속 가능, C#/Java 단일 상속 class CommonUnit1 { protected: string _name; int _hp; int _damage; public: CommonUnit1() { } CommonUnit1(string name, int damage) { _name = name; _damage = damage; } ~CommonUnit1() {} void Move() { cout << "[" << _name.c_str() << "] 님이 이동을 합니다. " << endl; } // 상속하고 있는 모든 클래스에서 사용 void Attack() { cout << "[" << _name.c_str() << "] 님이 " << _damage << "만큼의 공격을 합니다. " << endl; } void Hit(int damage) { cout << "[" << _name.c_str() << "] 님이 " << damage << " 데미지를 입었습니다. " << endl; } void Die() { cout << "[" << _name.c_str() << "] 님이 사망했습니다. " << endl; } }; class Player1 : public CommonUnit1 { private: int _level; int _gravity; string _talkMessage; public: Player1(string name, int damage, string talkMessage) : CommonUnit1(name, damage) { _talkMessage = talkMessage; cout << "[" << _name.c_str() << "] 님이 접속하였습니다." << endl; Talk(); }; ~Player1() {}; void Talk() { cout << "[" << _name.c_str() << "] 메시지 : " << _talkMessage.c_str() << endl; } // 오버라이드 // 부모 클래스에서 가지고 있는 Attack()을 호출 않하고 재정의 // 같은 메소드명(같은 자료형 인수) void Attack() { cout << "[" << _name.c_str() << "] 님이 총을 쏘면서 피하면서 " << _damage << "만큼의 공격을 합니다. " << endl; } void Jump() { cout << "[" << _name.c_str() << "] 님이 점프를했습니다. " << endl; } }; class Monster1 : public CommonUnit1 { private: int _skillType; public: Monster1(string name, int damage, int skillType) : CommonUnit1(name, damage) { _skillType = skillType; cout << "[" << _name.c_str() << "] 님이 나타났습니다." << endl; }; ~Monster1() {}; void Skill() { cout << "[" << _name.c_str() << "] 님이 " << _skillType << "타입의 스킬을 사용합니다. " << endl; } }; class Creature1 : public CommonUnit1 { private: int _time; public: Creature1(string name, int damage, int time) : CommonUnit1(name, damage) { _time = time; cout << "[" << _name.c_str() << "] 님이 소환 됐습니다." << endl; Timer(); }; ~Creature1() {}; // 오버라이드 void Hit(int damage) { // 부모의 Hit 메소드를 호출 CommonUnit1::Hit(damage); Attack(); // 크리쳐는 공격을 받으면 공격을 시도함 } void Timer() { cout << "[" << _name.c_str() << "] 님이 " << _time << " 시간뒤에 소멸됩니다." << endl; } }; class ExtendsStudy1 { public: ExtendsStudy1() { Start(); } void Start() { Player1* player = new Player1("티거", 30, "저는 푸우의 친구 티거입니다!!"); player->Move(); player->Attack(); player->Hit(10); player->Die(); delete player; Monster1* monster = new Monster1("오우거", 20, 2); monster->Move(); monster->Attack(); monster->Skill(); monster->Hit(30); monster->Die(); delete monster; Creature1* creature = new Creature1("멀록", 5, 10); creature->Move(); creature->Hit(5); creature->Die(); delete creature; } }; void main() { ExtendsStudy1* object = new ExtendsStudy1(); delete object; }
댓글 0개
| 엮인글 0개
285개(7/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
165 | [C#] 네임스페이스 (namespace) | 푸딩뱃살 | 3405 | 2015.11.14 17:34 |
164 | [C#] 인터페이스 (Interface) | 푸딩뱃살 | 2196 | 2015.11.13 18:17 |
163 | [C/C++] Static(정적) 멤버 변수, 메소드 | 푸딩뱃살 | 2473 | 2015.11.13 10:32 |
162 | [C/C++] 추상 클래스 - 오버라이드 / 업/다운캐스팅(형변환) / virtual(가상함수) | 푸딩뱃살 | 4726 | 2015.11.12 02:00 |
>> | [C/C++] 메소드 오버라이드 | 푸딩뱃살 | 1993 | 2015.11.12 01:51 |
160 | [C#] 상속 (with Unity) | 푸딩뱃살 | 5454 | 2015.11.10 16:25 |
159 | [C/C++] 클래스 상속 | 푸딩뱃살 | 2245 | 2015.11.10 14:08 |
158 | [C/C++] 객체 활용 | 푸딩뱃살 | 2471 | 2015.11.09 21:28 |
157 | [C#] C# 객체 클래스 | 푸딩뱃살 | 3568 | 2015.11.08 15:51 |
156 | [C/C++] 생성자 / 소멸자 / 오버로드 | 푸딩뱃살 | 2202 | 2015.11.07 01:23 |
155 | [C/C++] 클래스 선언/정의, 객체 생성 | 푸딩뱃살 | 3567 | 2015.11.06 14:05 |
154 | [C/C++] 로또 프로그램 | 푸딩뱃살 | 2284 | 2015.11.06 12:00 |
153 | [C/C++] 2차원 동적 객체 배열 활용 | 푸딩뱃살 | 3650 | 2015.11.06 00:47 |
152 | [C/C++] 2차원 배열 | 푸딩뱃살 | 2403 | 2015.11.06 00:30 |
151 | [C/C++] 2차원 포인터 | 푸딩뱃살 | 2052 | 2015.11.06 00:26 |
150 | [C/C++] 동적할당 | 푸딩뱃살 | 1805 | 2015.11.05 11:23 |
149 | [C/C++] 당신의 프로그래밍에 디버깅 더하기 : Visual C++ 디버깅 기초에서 고급까지 | 푸딩뱃살 | 1451 | 2015.11.05 11:20 |
148 | [C/C++] 포인터와 배열의 이해 | 푸딩뱃살 | 2013 | 2015.11.04 23:54 |
147 | [C/C++] 포인터 | 푸딩뱃살 | 2192 | 2015.11.04 15:14 |
146 | [C/C++] 일반 함수와 메소드간의 차이 | 푸딩뱃살 | 2430 | 2015.11.03 23:38 |