콘텐츠로 건너뛰기

Object-Oriented Programming in C


정의 : 객체 개념을 가지고 프로그래밍 한다는 의미이다.

클래스의 사전적 의미 : a group of people, animals or things that have similar characteristics or qualities

  • 클래스는 컨테이너를 말하고 연관되어 있는 변수(variable=data=속성=상태)와 함수(function=logic=method=행위)의 모음이다.
  • 클래스(class)는 객체 지향 프로그래밍(OOP)에서 특정 객체를 생성하기 위해 변수와 메소드를 정의하는 일종의 틀이다. 객체를 정의 하기 위한 상태(멤버변수)와 메서드(함수)로 구성된다. <- wiki
  • 클래스는 블랙박스이며 파라메타와 logic의 모임이다.
  • 클래스는 설계도이다.

인스턴스(Instance)의 사전적 의미 : 실제 사례. 

  • 인스턴스는 클래스를 복제한 것이다.
  • 인스턴스는 블랙박스의 파라메타값을 결정해 놓는 것이다.
  • 인스턴스는 설계도를 참조해서 제품을 만든 것이다.

인스턴스 던젼은 내부 맵 및 보상이 바뀌지만 이 던젼의 설계도인 클래스는 변하지 않는다.

Encapsulation : 클래스에 데이타 및 펑션을 실장하는 기능

Inheriatance : 기존 클래스에 기반하여 클래스의 재사용 및 코드 통합을 위해서 새로운 클래스를 정의하는 기능

Polymorphism : run-time시 적절한 인터페이스에 맞도록 object를 대체해주는 기능

1. Encapsulation(캡슐화)

캡슐로 내용물을 가두어 놓으면 내용물을 보호할 수 있다. 캡슐속의 알약은 자신만의 특성 및 기능을 가지고 있다. 즉 클래스를 만들어 쓰라는 의미이다.

2. Inheriatance(상속) 

항암제 캡슐이 있는데 여기 부작용이 소화불량이다. 그럼 소화제를 넣으면 되지 소화불량을 해소하기 위한 항암제를 따로 만들 이유는 없다.

3. Polymorphism(다형성)

function_A가 있는데 function_A의 이름은 그대로 사용하지만 아규먼트만 달리해서 쓰면 펑션의 기능이 달라진다는 것이다. 사람이 손가락으로 V를 만들어도 상황에 따라 2 개 달라는 지, 아니면 승리! 라는 것을 표현하는 것인지는 상황에 따라 달라진다. 마찬가지고 프로그래밍에서도 아규먼트의 갯수 또는 자료형을 보면 이 평션이 어떤것으로 작동해야 하는지 알 수 있다는 의미이다.

Encapsulation 예제

shape.h source file

#ifndef SHAPE_H
#define SHAPE_H

#include <stdint.h>

/* Shape's attributes... */
typedef struct {
int16_t x; /* x-coordinate of Shape's position */
int16_t y; /* y-coordinate of Shape's position */
} Shape;
/* Shape's operations (Shape's interface)... */
void Shape_ctor(Shape * const me, int16_t x, int16_t y);
void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy);
int16_t Shape_getX(Shape * const me);
int16_t Shape_getY(Shape * const me);
#endif /* SHAPE_H */
// shape.c source file

#include "shape.h" /* Shape class interface */
/* constructor implementation */
void Shape_ctor(Shape * const me, int16_t x, int16_t y) {
 me->x = x;
 me->y = y;
}
/* move-by operation implementation */
void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy) {
 me->x += dx;
 me->y += dy;
}
/* "getter" operations implementation */
int16_t Shape_getX(Shape * const me) {
 return me->x;
}
int16_t Shape_getY(Shape * const me) {
 return me->y;
}
// main.c source file

#include "shape.h" /* Shape class interface */
#include <stdio.h> /* for printf() */
int main() {
 Shape s1, s2; /* multiple instances of Shape */
 Shape_ctor(&s1, 0, 1);
 Shape_ctor(&s2, -1, 2);
 printf("Shape s1(x=%d,y=%d)\n", Shape_getX(&s1), Shape_getY(&s1));
 printf("Shape s2(x=%d,y=%d)\n", Shape_getX(&s2), Shape_getY(&s2));
 Shape_moveBy(&s1, 2, -4);
 Shape_moveBy(&s2, 1, -2);
 printf("Shape s1(x=%d,y=%d)\n", Shape_getX(&s1), Shape_getY(&s1));
 printf("Shape s2(x=%d,y=%d)\n", Shape_getX(&s2), Shape_getY(&s2));
 return 0;
}

Inheriatance 예제

// rect.h source file

#ifndef RECT_H
#define RECT_H
#include "shape.h" /* the base class interface */
/* Rectangle's attributes... */
typedef struct {
 Shape super; /* <== inherits Shape */
 /* attributes added by this subclass... */
 uint16_t width;
 uint16_t height;
} Rectangle;
/* constructor prototype */
void Rectangle_ctor(Rectangle * const me, int16_t x, int16_t y,
 uint16_t width, uint16_t height);
#endif /* RECT_H */
// rect.c source file.

#include "rect.h"
/* constructor implementation */
void Rectangle_ctor(Rectangle * const me, int16_t x, int16_t y,
 uint16_t width, uint16_t height)
{
 /* first call superclass’ ctor */
 Shape_ctor(&me->super, x, y);
 /* next, you initialize the attributes added by this subclass... */
 me->width = width;
 me->height = height;
}
// main.c source file

#include "rect.h" /* Rectangle class interface */
#include <stdio.h> /* for printf() */
int main() {
 Rectangle r1, r2; /* multiple instances of Rect */
 /* instantiate rectangles... */
 Rectangle_ctor(&r1, 0, 2, 10, 15);
 Rectangle_ctor(&r2, -1, 3, 5, 8);
 printf("Rect r1(x=%d,y=%d,width=%d,height=%d)\n",
 r1.super.x, r1.super.y, r1.width, r1.height);
 printf("Rect r2(x=%d,y=%d,width=%d,height=%d)\n",
 r2.super.x, r2.super.y, r2.width, r2.height);
 /* re-use inherited function from the superclass Shape... */
 Shape_moveBy((Shape *)&r1, -2, 3);
 Shape_moveBy(&r2.super, 2, -1);
 printf("Rect r1(x=%d,y=%d,width=%d,height=%d)\n",
 r1.super.x, r1.super.y, r1.width, r1.height);
 printf("Rect r2(x=%d,y=%d,width=%d,height=%d)\n", r2.super.x, r2.super.y, r2.width, r2.height);
 return 0;
}

Polymorphism (Virtual Function)

// shape.h source file

 #ifndef SHAPE_H
 #define SHAPE_H
 #include <stdint.h>
 /* Shape's attributes... */
 struct ShapeVtbl; /* forward declaration */
 typedef struct {
(1) struct ShapeVtbl const *vptr; /* <== Shape's Virtual Pointer */
 int16_t x; /* x-coordinate of Shape's position */
 int16_t y; /* y-coordinate of Shape's position */
 } Shape;
 /* Shape's virtual table */
(2) struct ShapeVtbl {
(3) uint32_t (*area)(Shape const * const me);
(4) void (*draw)(Shape const * const me);
 };
 /* Shape's operations (Shape's interface)... */
 void Shape_ctor(Shape * const me, int16_t x, int16_t y);
 void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy);
 static inline uint32_t Shape_area(Shape const * const me) {
(5) return (*me->vptr->area)(me);
 }
 static inline void Shape_draw(Shape const * const me) {
(6) (*me->vptr->draw)(me);
 }
 /* generic operations on collections of Shapes */
(7) Shape const *largestShape(Shape const *shapes[], uint32_t nShapes);
(8) void drawAllShapes(Shape const *shapes[], uint32_t nShapes);
 #endif /* SHAPE_H */
// shape.c source file

 #include "shape.h"
 #include <assert.h>
 /* Shape's prototypes of its virtual functions */
(1) static uint32_t Shape_area_(Shape const * const me);
(2) static void Shape_draw_(Shape const * const me);
 /* constructor */
 void Shape_ctor(Shape * const me, int16_t x, int16_t y) {
(3) static struct ShapeVtbl const vtbl = { /* vtbl of the Shape class */
 &Shape_area_,
 &Shape_draw_
 };
(4) me->vptr = &vtbl; /* "hook" the vptr to the vtbl */
 me->x = x;
  me->y = y;
 }
 /* move-by operation */
 void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy) {
 me->x += dx;
 me->y += dy;
 }
 /* Shape class implementations of its virtual functions... */
 static uint32_t Shape_area_(Shape const * const me) {
(5) assert(0); /* purely-virtual function should never be called */
 return 0U; /* to avoid compiler warnings */
 }
 static void Shape_draw_(Shape const * const me) {
(6) assert(0); /* purely-virtual function should never be called */
 }
 /* the following code finds the largest-area shape in the collection */
 Shape const *largestShape(Shape const *shapes[], uint32_t nShapes) {
 Shape const *s = (Shape *)0;
 uint32_t max = 0U;
 uint32_t i;
 for (i = 0U; i < nShapes; ++i) {
(7) uint32_t area = Shape_area(shapes[i]); /* virtual call */
 if (area > max) {
 max = area;
 s = shapes[i];
 }
 }
 return s; /* the largest shape in the array shapes[] */
 }
 /* The following code will draw all Shapes on the screen */
 void drawAllShapes(Shape const *shapes[], uint32_t nShapes) {
 uint32_t i;
 for (i = 0U; i < nShapes; ++i) {
(8) Shape_draw(shapes[i]); /* virtual call */
 }
 }

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다