algorithm/문제 풀이

열혈 C++ 프로그래밍 클래스 정의 문제

ssoheeh 2021. 1. 10. 20:47

문제 04-3-2번

명함을 의미하는 NameCard 클래스 정의

 

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
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cstring>
using namespace std;
 
namespace COMP_POS {
    enum {
        CLERK, SENIOR, ASSIST, MANAGER
    };
    void ShowPositionInfo(int pos) {
        switch (pos) {
        case CLERK:
            cout << "사원" << endl << endl;
            break;
        case SENIOR:
            cout << "주임" << endl << endl;
            break;
        case ASSIST:
            cout << "대리" << endl << endl;
            break;
        case MANAGER:
            cout << "과장" << endl << endl;
            break;
        }
    }
}
class NameCard {
    char* name;
    char* companeyName;
    char* phonenumber;
    int pos;
 
public:
    NameCard(const char* n, const char* cn, const char* pn, int po) 
    :pos(po){
        name = new char[strlen(n) + 1];
        companeyName = new char[strlen(cn) + 1];
        phonenumber = new char[strlen(pn) + 1];
        strcpy(name, n);
        strcpy(companeyName, cn);
        strcpy(phonenumber, pn);
 
    }
 
    void ShowNameCardInfo() const {
        cout << "이름 : " << name << endl;
        cout << "회사 : " << companeyName << endl;
        cout << "전화번호 : " << phonenumber << endl;
        cout << "직급 : ";
        COMP_POS::ShowPositionInfo(pos);
        cout << endl;
    }
 
    ~NameCard() {
        delete[]name;
        delete[]companeyName;
        delete[]phonenumber;
    }
 
};
 
int main() {
    NameCard manClerk("Lee""ABCEng""010-1111-2222", COMP_POS::CLERK);
    NameCard manSenior("Hong""OrangeEng""010-3333-4444", COMP_POS::SENIOR);
    NameCard manAssist("Kim""SoGoodComp""010-5555-6666", COMP_POS::ASSIST);
    manClerk.ShowNameCardInfo();
    manSenior.ShowNameCardInfo();
    manAssist.ShowNameCardInfo();
    return 0;
}
cs

 

아직 C에서 문자열 기반으로 한 입출력이 헷갈린다.

자바에서는 String으로 편하게 써서 그런가

char 배열과 포인터 복습 꾸준히 해야 할 듯!