회원 로그인
정보기억 정보기억에 체크할 경우 다음접속시 아이디와 패스워드를 입력하지 않으셔도 됩니다.
그러나, 개인PC가 아닐 경우 타인이 로그인할 수 있습니다.
PC를 여러사람이 사용하는 공공장소에서는 체크하지 마세요.
소셜네트워크 서비스를 통해서 로그인하시면 별도의 로그인 절차없이 회원서비스를 이용하실 수 있습니다.


최근 게시물

1.노션에서 작성 중

1.노션에서 작성 중

개편하기 전까지 노션에서 작성 중

2024.04.04//read more

2.ChatGPT

2.ChatGPT

OpenAI로 대규모 언어 모델대화형...

2023.03.16//read more

3.노코딩 게임 엔진 - 빌..

3.노코딩 게임 엔진 - 빌..

빌드 지원안드로이드iOS윈도우즈특이사...

2023.03.14//read more

4.(완료) 미접속 회원 정..

4.(완료) 미접속 회원 정..

[완료] 36명의 회원을 정리하였습니...

2023.02.16//read more

5.매뉴얼 플러스 - 전자제..



안정적인 DNS 서비스 DNSEver
DNS Powered by DNSEver.com


클래스 상속

푸딩뱃살 | 2015.11.10 14:08 | 조회 2257
C++ 클래스 상속

상속 사용 목적
C++, C#의 상속 전 클래스를 통해 상속의 필요성에 대해 알아 본다.

상속 전
/*
상속 사용 목적
C++, C#의 상속 전 클래스를 통해 상속의 필요성에 대해 알아 본다.
*/

#include 
#include 
#include 
#include 

using namespace std;

/* 
Player1, Monster1, Creature 클래스에서 name, damage, hp, Move(), Attack(), Die() 등이
속성 중복되어 이것을 줄이기 위해 상속 사용
*/

class Player1{
private:
    // 속성 반복
    string _name;
    int _hp;
    int _damage;
    int _level;
    int _gravity;
    string _talkMessage;
public:
    Player1(string name, string talkMessage, int damage){
        _name = name;
        _talkMessage = talkMessage;
        _damage = damage;
        _hp = 100;
        _level = 1;
        _gravity = 1;

        cout << "[" << _name << "] 님이 접속" << endl;
        Talk();
    }

    ~Player1() {}

    void Talk() {
        cout << "[" << _name << "] 메시지 : " << _talkMessage << endl;
    }
    // 메소드 반복
    void Move() {
        cout << "[" << _name << "] 님이 이동" << endl;
    }

    void Attack() {
        cout << "[" << _name << "] 님이 " << _damage << " 만큼의 공격" << endl;
    }

    void Hit(int damage) {
        cout << "[" << _name << "] 님이 " << damage << " 데미지" << endl;
    }

    void Die() {
        cout << "[" << _name << "] 님이 사망" << endl;
    }

    void Jump() {
        cout << "[" << _name << "] 님이 점프" << endl;
    }
};

class Monster1 {
private:
    // 속성 반복
    string _name;
    int _hp;
    int _damage;
    int _skillType;
public:
    Monster1(string name, int skillType, int damage){
        _name = name;
        _skillType = skillType;
        _damage = damage;
        _hp = 100;

        cout << "[" << _name << "] 님이 나타났다" << endl;
    }

    ~Monster1() {}

    // 메소드 반복
    void Move() {
        cout << "[" << _name << "] 님이 이동" << endl;
    }

    void Hit(int damage) {
        cout << "[" << _name << "] 님이 " << damage << " 데미지" << endl;
    }

    void Die() {
        cout << "[" << _name << "] 님이 사망" << endl;
    }

    void Skill() {
        cout << "[" << _name << "] 님이 점프" << _skillType << " 타입의 스킬을 사용" << endl;
    }
};

class Creature1{
private:
    // 속성 반복
    string _name;
    int _hp;
    int _damage;
    int _time;
public:
    Creature1(string name, int time, int damage){
        _name = name;
        _time = time;
        _hp = 100;
        _damage = damage;

        cout << "[" << _name << "] 님이 소환" << endl;
        Timer();
    }
    // 메소드 반복
    void Move() {
        cout << "[" << _name << "] 님이 이동" << endl;
    }

    void Hit(int damage) {
        cout << "[" << _name << "] 님이 " << damage << " 데미지" << endl;
    }

    void Die() {
        cout << "[" << _name << "] 님이 사망" << endl;
    }

    void Timer() {
        cout << "[" << _name << "] 님이 " << _time << " 시간 뒤에 소멸" << endl;
    }

};

void main1() {
    Player1* player = new Player1("푸딩", "저는 푸딩", 100);
    player->Move();
    player->Attack();
    player->Hit(10);
    player->Die();
    delete player;

    Monster1* monster = new Monster1("오우거", 2, 10);
    monster->Move();
    monster->Skill();
    monster->Hit(30);
    monster->Die();
    delete monster;

    Creature1* creature = new Creature1("멀록", 10, 10);
    creature->Move();
    creature->Hit(5);
    creature->Die();
    delete creature;
}

상속 후
상속 구조로 변경
0. 먼저 클래스 코딩 시 속성 중복 작성
1. 공통된 이름의 클래스를 만듬
2. 중복된 속성,
3. 중복된 메소드
4. 상속할 클래스
5. 중복된 속성, 메소드 제거
6. 중복된 생성자

장점) 중복 된 코드 간소화
...
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>

using namespace std;

// 1. 중복되는 부분에 대한 공통적인 이름의 클래스를 만듬
class Character2 {
protected:  // 기초(부모) 클래스>파생(자식) 클래스, 자식 클래스에서 부모 클래스 접근할 수 있는
    // 2. 중복된 속성을 골라냄
    string _name;
    int _hp;
    int _damage;
public:
    // 6. 각 클래스의 생성자의 중복 요소를 가지고 생성자를 만듬
    Character2(string name, int damage){
        _name = name;
        _damage = damage;
        _hp = 100;  // 공통 부분 및 이미 초기화라 인수 안받아도 됨
                    // 각 클래스의 hp가 다르다면 인수에 int hp 넣음
    }

    // 3. 중복된 메소드를 골라냄

    void Move() {
        cout << "[" << _name << "] 님이 이동" << endl;
    }

    void Hit(int damage) {
        cout << "[" << _name << "] 님이 " << damage << " 데미지" << endl;
    }

    void Die() {
        cout << "[" << _name << "] 님이 사망" << endl;
    }
};

// 4. 상속할 클래스를 지정
// 5. 중복되었던 요소들 제거
class Player2 : public Character2{
private:  // 자신의 클래스에서만 접근
    int _level;
    int _gravity;
    string _talkMessage;
public:  // 모두 접근
    // ~~ : Character1(name, damage) -> 생성자를 부모 생성자(Character1())로 먼저 호출
    Player2(string name, string talkMessage, int damage) : Character2(name, damage) {
        _talkMessage = talkMessage;
        _level = 1;
        _gravity = 1;

        cout << "[" << _name << "] 님이 접속" << endl;
        Talk();
    }

    ~Player2() {}

    // 3. 중복된 메소드를 골라냄

    void Talk() {
        cout << "[" << _name << "] 메시지 : " << _talkMessage << endl;
    }

    void Attack() {
        cout << "[" << _name << "] 님이 " << _damage << " 만큼의 공격" << endl;
    }

    void Jump() {
        cout << "[" << _name << "] 님이 점프" << endl;
    }
};

//  Monster2 클래스는 Character1 클래스를 상속
class Monster2 : public Character2 {
private:
    int _skillType;
public:
    Monster2(string name, int skillType, int damage) : Character2(name, damage) {
        _skillType = skillType;

        cout << "[" << _name << "] 님이 나타났다" << endl;
    }

    ~Monster2() {}

    // 3. 중복된 메소드를 골라냄

    void Skill() {
        cout << "[" << _name << "] 님이 점프" << _skillType << " 타입의 스킬을 사용" << endl;
    }
};

//  Creatrue2 클래스는 Character1 클래스를 상속
class Creature2 : public Character2{
private:
    int _time;
public:
    Creature2(string name, int time, int damage) : Character2(name, damage) {
        _time = time;

        cout << "[" << _name << "] 님이 소환" << endl;
        Timer();
    }

    void Timer() {
        cout << "[" << _name << "] 님이 " << _time << " 시간 뒤에 소멸" << endl;
    }

};

void main() {
    Player2* player = new Player2("푸딩", "저는 푸딩", 100);
    player->Move();
    player->Attack();
    player->Hit(10);
    player->Die();
    delete player;

    Monster2* monster = new Monster2("오우거", 2, 10);
    monster->Move();
    monster->Skill();
    monster->Hit(30);
    monster->Die();
    delete monster;

    Creature2* creature = new Creature2("멀록", 10, 10);
    creature->Move();
    creature->Hit(5);
    creature->Die();
    delete creature;
}
285개(1/15페이지)
프로그래밍
번호 제목 글쓴이 조회 날짜
285 [Python] 동적 import - 모듈을 변수로 받아오기 푸딩뱃살 412 2022.10.27 10:45
284 [Python] 파이썬 3.7.7과 3.9.7의 os.path.expanduser() 차이 푸딩뱃살 455 2022.08.18 12:22
283 [Python] error: Microsoft Visual C++ 9.0 is required. 첨부파일 푸딩뱃살 684 2022.08.03 13:35
282 [Python] pyscript 첨부파일 푸딩뱃살 453 2022.06.09 11:21
281 [Python] float is / float not is 푸딩뱃살 593 2022.03.02 15:03
280 [Python] 이터널 문자열 f 푸딩뱃살 833 2022.01.27 16:35
279 [Python] is와 ==의 차이 푸딩뱃살 485 2021.11.25 15:54
278 [Python] Error: ImportError: file line 1: Ba 푸딩뱃살 916 2021.11.16 11:24
277 [Python] 파이썬 디컴파일 - uncompyle6 첨부파일 푸딩뱃살 774 2021.11.10 14:46
276 [Python] 파이썬 확장자 설명 푸딩뱃살 550 2021.11.03 14:38
275 [참고] 웹 fbx 뷰어 푸딩뱃살 470 2021.10.19 15:46
274 [Python] enumerate() 푸딩뱃살 500 2021.10.13 14:44
273 [Python] 아나콘다에서 가상 환경 첨부파일 푸딩뱃살 719 2020.11.21 00:26
272 [Python] pip로 설치 때 퍼미션 에러 사진 첨부파일 푸딩뱃살 1265 2020.06.06 17:13
271 [Python] OpenCV 10-3. 이미지 Thresholding - Otsu's Binarizatio 사진 푸딩뱃살 673 2020.06.05 14:01
270 [Python] OpenCV 10-2. 이미지 Thresholding - Adaptive Threshold 사진 푸딩뱃살 694 2020.06.05 13:58
269 [Python] OpenCV 10-1. 이미지 Thresholding 사진 푸딩뱃살 578 2020.06.05 13:56
268 [Python] OpenCV 9-2. 색 추적 푸딩뱃살 772 2020.06.02 23:29
267 [Python] OpenCV 9-1. 색공간 바꾸기 푸딩뱃살 639 2020.06.02 23:27
266 [Python] OpenCV 8-3. 이미지 비트 연산 사진 푸딩뱃살 528 2020.06.02 23:21