C 語言, 物件導向, 面向對象 @ 071's blog :: 隨意窩 Xuite日誌
    1. 沒有新回應!
  • 201908281726C 語言, 物件導向, 面向對象

    用C語言, 設計物件導向, 代碼如下

    #
    # Makefile
    # Created on: 2019/08/28
    # Author: Antony.Lin
    #
    all: myapp

    CC = gcc
    INCLUDE = .
    CFLAGS = -g

    myapp: main.o rect.o circle.o
    $(CC) -o myapp main.o rect.o circle.o

    main.o: main.c shape.h
    $(CC) -I$(INCLUDE) $(CFLAGS) -c main.c

    rect.o: rect.c shape.h
    $(CC) -I$(INCLUDE) $(CFLAGS) -c rect.c

    circle.o: circle.c shape.h
    $(CC) -I$(INCLUDE) $(CFLAGS) -c circle.c

    #end of Makefile

     

    /*
    * shape.h
    * Created on: 2019/08/28
    * Author: Antony.Lin
    */
    #ifndef SHAPE_H
    #define SHAPE_H

    #include <stdint.h>

    typedef struct Shape Shape;
    struct Shape{
    int16_t x;
    int16_t y;
    int (*area)(Shape* shape);
    void (*draw)(Shape* shape);
    };

    Shape* CreateRectangle(int16_t x, int16_t y, int16_t w, int16_t h);
    void DestroyRectangle(Shape* s);

    Shape* CreateCircle(int16_t x, int16_t y, int16_t radius);
    void DestroyCircle(Shape* s);

    #endif /* SHAPE_H */

    // end of shape.h

     

    /*
    * rect.c
    * Created on: 2019/08/28
    * Author: Antony.Lin
    */
    #include "shape.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    typedef struct Rectangle{
    Shape public;
    int16_t width;
    int16_t height;
    } Rectangle;

    static int area(Shape* s){
    Rectangle* shape = (Rectangle*) s;
    return (int)shape->width * (int)shape->height;
    }

    static void draw(Shape* s){
    Rectangle* shape = (Rectangle*)s;
    printf("Rectangle draw at x:%d y:%d , w:%d h:%d\n",shape->public.x,shape->public.y,shape->width,shape->height);
    }

    Shape* CreateRectangle(int16_t x, int16_t y, int16_t w, int16_t h){
    Rectangle* shape = malloc(sizeof(Rectangle));
    if (shape != NULL){
    shape->public.x = x;
    shape->public.y = y;
    shape->width = w;
    shape->height = h;
    shape->public.area = area;
    shape->public.draw = draw;
    }
    return (Shape*) shape;
    }

    void DestroyRectangle(Shape* s){
    Rectangle* shape = (Rectangle*) s;
    free(shape);
    }
    // end of rect.c

     

    /*
    * circle.c
    * Created on: 2019/08/28
    * Author: Antony.Lin
    */
    #include "shape.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    typedef struct Circle{
    Shape public;
    int16_t radius;
    } Circle;

    static int area(Shape* s){
    Circle* shape = (Circle*) s;
    return (int)((int)shape->radius * (int)shape->radius * 3.14159);
    }

    static void draw(Shape* s){
    Circle* shape = (Circle*)s;
    printf("Circle draw at x:%d y:%d , radius:%d\n",shape->public.x,shape->public.y,shape->radius);
    }

    Shape* CreateCircle(int16_t x, int16_t y, int16_t radius){
    Circle* shape = malloc(sizeof(Circle));
    if (shape != NULL){
    shape->public.x = x;
    shape->public.y = y;
    shape->radius = radius;
    shape->public.area = area;
    shape->public.draw = draw;
    }
    return (Shape*) shape;
    }

    void DestroyCircle(Shape* s){
    Circle* shape = (Circle*) s;
    free(shape);
    }
    // end of circle.c

     

    /*
    * main.c
    * Created on: 2019/08/28
    * Author: Antony.Lin
    */
    #include "shape.h"
    #include <stdio.h>

    int main()
    {
    Shape* rect = CreateRectangle(2,3,4,5);
    rect->draw(rect);
    printf("Rectangle's Area : %d\n", rect->area(rect));
    DestroyRectangle(rect);

    Shape* circle = CreateCircle(2,3,4);
    circle->draw(circle);
    printf("Circle's Area : %d\n", circle->area(circle));
    DestroyCircle(circle);
    return 0;
    }
    // end of main.c

     

     

    lighttpd https 解析|日誌首頁|py module上一篇lighttpd https 解析下一篇py module
    回應