카테고리

asm (27) bootloader_x86_grub (1) C (92) compile (11) config (76) CPP (13) CSS (1) debugging (7) gimp (1) Go (1) html (1) Java (1) JavaScript (1) kernel (19) LibreOffice (3) Linux system progamming (21) MFC (1) opencv (4) OpenGL (1) PHP (1) Python (4) qemu (29) shell (3) socket (7) troubleshooting (2) ubuntu18.04 (2) windows (1)

2018/12/05

OOP 다향성

다향성

다형성 = 하나 이상의 형식을 취하는 능력

(객체에는 두 가지 이상의 유형이 있다)
- 부모 인터페이스를 통해 클래스를 사용할 수 있다.
- 자식 클래스는 부모 클래스의 일부 동작을 재정의 할 수 있다.

다형성은 추상 동작을 정의하고 사용할 수 있게 해준다.
- 추상 동작은 기본 클래스의 인터페이스에서 정의되며 하위 클래스에서 구현된다.
- 추상 또는 가상으로 선언

단어 하나의 각기 다른 해석.

Example 1
int main(){
    Rectangle rect;
    Triangle trgl;
    Polygon * ppoly1 = ▭
    Polygon * ppoly2 = &trgl;
    pply1->set_values (4,5);
    pply2->set_values (4,5);
    cout << rect.area() << '\n';
    cout << trgl.area() << '\n';
    return 0;
}

#include <iostream>
usnig namespace std;

class Polygon {
    protected:
        int width, height;
    public:
        void set_values (int a, int b)
            {width=a; height=b; }
};

class Rectangle: public Polygon {
    public:
        int area()
         {return width*height:}
};

class Triangle: public Polygon {
    public:
        int area()
         { return width*height/2; }
};

댓글 없음:

댓글 쓰기