VioletaBabel

8일 : 상속과 다형성 본문

BCA/1. C,C++,C#
8일 : 상속과 다형성
Beabletoet 2018. 2. 19. 11:33
1
2
3
4
5
6
7
8
9
10
11
12
13
class Unit
{
    public:
    virtual void a() {cout<<"안녕";}
};
class Hi : public Unit
{
    void a(){cout<<"반가워";}
};
//같은 식으로 만들었을 때 
//Unit *p = &b; 하여 p->a(); 하면 자식의 a()가 뜨고, virtual가 없으면 부모의 a()가 뜬다.
//그리고 Unit *p = &b; 는 가능하지만, Unit m; Hi *l = &m; 은 안된다. 
//부모의 포인터에 자식을 넣을 수는 있지만, 자식의 포인터에 부모를 넣을 수 없다.
cs

--

1
2
3
4
5
6
7
8
9
10
11
class Unit
{
    public:
    virtual void a() = 0//순수 가상함수
};
class Hi : public Unit
{
    void a(){cout<<"반가워";}
};
//부모에게 순수 가상함수가 있는 
//상속받았을 때 a()를 무조건 새로 구현해야함.
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
#define QCnt 10 // 맞춰야 할 표적 갯수
class Point
// 부모 클래스
protected:
    int x;
    int y;
    int die = 0// 명중당했는지 판별
public:
    bool a; // 원인지 사각형인지 판별
    virtual bool Collision(int _x, int _y) = 0// 충돌 판정 순수 가상함수
    Point()
    { // x와 y의 범위는 1부터 7까지
        x = (rand() % 7)+1;
        y = (rand() % 7)+1;
    }
    virtual void say() = 0//디버깅용 값 출력 순수 가상함수
};
class Circle : public Point
// 원 생성
private:
    int r; // 반지름
public:
    Circle() : Point() // 부모의 생성자 상속
    {
        r = (rand() % 2+ 2// 반지름은 2부터 3까지
        a = 0// 원이다!!!!
    }
    bool Collision(int _x, int _y)
    {
        if (die); // 이미 명중당한 아이이면 그냥 끝낸다.
        else if ((_x - x)*(_x - x) + (_y - y)*(_y - y) < (r*r))
        {
            this->die = 1// 명중당함을 표시
            return 1// 명중당했다고 알려줌
        }
        return 0;
    }
    void say() { if(!die) cout << "x : " << x << " / y : " << y << " / r : " << r << endl; } // 값 출력
};
class Rectangle : public Point
// 사각형 생성
private:
    int w; // 너비
    int h; // 높이
public:
    Rectangle() : Point() // 부모의 생성자 상속
    {
        w = (rand() % 4+ 2// 너비와 높이는 2부터 5까지
        h = (rand() % 4+ 2;
        a = 1// 사각형이다!!!
    }
    bool Collision(int _x, int _y)
    {
        if (die); // 이미 명중당한 아이면 그냥 끝낸다.
        else if (this->< _x && this->< _y && this->x+> _x && this->y+> _y)
        {
            this->die = 1// 명중당함을 표시
            return 1// 명중당했다고 알려줌
        }
        return 0;
    }
    void say() { if (!die) cout << "x : " << x << " / y : " << y << " / w : " << w << " / h : " << h << endl; } // 값 출력
};
int main()
{
    srand(time(NULL));
    Point* shape[QCnt]; // 표적 생성
    int iCAllCnt = 0, iRAllCnt = 0, iCCnt = 0, iRCnt = 0, x, y;//AllCnt는 각 표적 갯수, Cnt는 맞춘 갯수
    for (int i = 0; i < 10++i)
        if (rand() % 2 == 0// 사각형과 원을 랜덤으로 선택해 생성
        {
            shape[i] = new Rectangle;
            ++iRAllCnt;
        }
        else
        {
            shape[i] = new Circle;
            ++iCAllCnt;
        }
    for (int i = 0; i < 10++i)
    {
        //for (int i = 0; i < 10; ++i)//debug
        //    shape[i]->say(); 
        cout << "\nx 값을 입력해주세요 -> ";
        cin >> x;
        cout << "y 값을 입력해주세요 -> ";
        cin >> y;
        for (int i = 0; i < 10++i)
        {
            if (shape[i]->Collision(x, y) == 1// 충돌했는지 판별
            {
                if (shape[i]->a)
                    ++iRCnt;
                else
                    ++iCCnt;
            }
        }
        cout << "circle : " << iCCnt << " / " << iCAllCnt << "\nrectangle : " << iRCnt << " / " << iRAllCnt << "\nscore : " << iRCnt + iCCnt << " / " << QCnt << endl;
        if (iRCnt + iCCnt == QCnt) // 다 명중 시 그냥 종료
            break;
    }
    //for (int i = 0; i < 10; ++i)//debug용
    //    shape[i]->say();
    cout << "\n게임이 끝났습니다!" << endl;
}
cs

--


Comments