본문 바로가기

프로그래밍/C++

C++ 클래스 실습 예제 - Hero


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
 
using namespace std;
 
int main() {
    int i_array_size;
    cin >> i_array_size;
    int *ia_first = new int[i_array_size];
 
    for (int i = 0; i < i_array_size; i++)
        cin >> ia_first[i];
    for (int i = 0; i < i_array_size; i++)
        cout << i+1 << " : " << ia_first[i] << endl;
 
    delete[] ia_first;
    return 0;
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
 
using namespace std;
 
int main() {
    string s_a = "홍익";
    string s_b = "대학교";
 
    cout << s_a << endl;
    cout << s_a + s_b << endl;
 
    cout << "s_a의 길이 : " << s_a.length() << endl;
 
    if (s_a == s_b)
        cout << "같다" << endl;
    else
        cout << "다르다" << endl;
 
    s_a.append("대학교");
    cout << "s_a : " << s_a << endl;
 
    s_b.insert(0"홍익");
    cout << "s_b : " << s_b << endl;
 
    s_a.clear();
    if (s_a.length() > 0)
        cout << "s_a : " << s_a << endl;
    else
        cout << "s_a가 비어있음" << endl;
 
    return 0;
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
 
using namespace std;
 
class Student {
private:
    string s_name;
    int i_age;
public:
    Student(string s_name, int i_age) {
        this->s_name = s_name;
        this->i_age = i_age;
    }
 
    void 신상공개() {
        cout << "이름 : " << s_name << endl;
        cout << "나이 : " << i_age << endl;
    }
};
 
int main() {
    Student sehan("세한"21);
    sehan.신상공개();
 
    return 0;
}
cs







<Hero.cpp>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "Hero.h"
#include <iostream>
#include <string>
 
using namespace std;
 
Hero::Hero(string s_name) : i_hp(100), i_attack(10), i_defense(2) {
    this->s_name = s_name;
    cout << s_name << " 등장!" << endl;
    //fu_make_skill_list();
}
 
Hero::~Hero() {
}
 
// 스킬 리스트 초기화
void Hero::fu_make_skill_list()
{
    for (int i = 0; i < 10; i++) {
        this->sa_skill_list[i] = "없음";
    }
}
 
int Hero::fu_print_status(int n)
{
    cout << '\n';
    switch (n) {
    case 1:
        cout << "< 상태 설명 >\n";
        cout << "이름 : " << s_name << endl;
        cout << "체력 : " << i_hp << endl;
        cout << "공격력 : " << i_attack << endl;
        cout << "방어력 : " << i_defense << endl;
        break;
    case 2:
        cout << "< 스킬 목록 >\n";
        for (int i = 0; i < 10; i++) {
            cout << "스킬 " << i + 1 << "번 : " << sa_skill_list[i] << endl;
        }
        break;
    case 3:
        cout << "< 스킬 습득 >\n";
        for (int i = 0; i < 10; i++) {
            if (!this->sa_skill_list[i].length()) {
                this->sa_skill_list[i] = this->sa_skills[i];
                break;
            }
        }
        break;
    case 99:
        cout << "< 행동 종료 >\n";
        return 0;
    default:
        cout << "목록에 없는 번호를 선택했습니다.\n";
    }
}
 
cs


< Hero.h>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma once
#include <string>
 
using namespace std;
 
class Hero
{
private:
    string s_name;
    int i_hp, i_attack, i_defense;
    string sa_skill_list[10];
    string sa_skills[100= {"연속베기""방어""죽은척하기", };
 
public:
    Hero(string s_name);
    ~Hero();
 
    int fu_print_status(int n);
 
private:
    void fu_make_skill_list();
};
 
 

cs


<main.cpp>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include "Hero.h"
 
using namespace std;
 
int main() {
    string s_name;
    cin >> s_name;
 
    Hero sehan(s_name);
 
    int i_action;
    do {
        cout << "\n=================\n< 행동 목록 >\n";
        cout << "1 : 상태 설명\n";
        cout << "2 : 스킬 목록\n";
        cout << "3 : 스킬 습득\n";
        cout << "\n99 : 행동 종료\n";
        cout << "행동번호 입력 : ";
        cin >> i_action;
    } while (sehan.fu_print_status(i_action));
    return 0;
}
cs


'프로그래밍 > C++' 카테고리의 다른 글

C++ 문법 - 객체 배열  (0) 2018.02.10
C++ 문법 - 동적할당  (0) 2018.02.10
C++ 문법 - 레퍼런스  (0) 2018.01.29
C++ 문법 - 포인터  (0) 2018.01.28
C++ 문법 - 상수  (0) 2018.01.28