zoukankan      html  css  js  c++  java
  • C语言实现OOP 版本3 :简化代码

    我倒是不追求代码和C++相似,但是应该追求简洁的代码,下面是一个新的尝试


    shape.h

    #ifndef SHAPE_H
    #define SHAPE_H
    
    typedef struct shape_t 
    {
        void *shapeData;
        void (*area)(void *);
        void (*release)(void *);
    }Shape;
    
    void release(void *shape);
    
    #endif

    shape.c

    #include <stdlib.h>
    #include "shape.h"
    
    void release(void *shape)
    {
        free(((Shape*)shape)->shapeData);
        free(shape);
    }

    circle.h

    #ifndef CIRCLE_H
    #define CIRCLE_H
    
    #include "shape.h"
    
    typedef struct 
    {
        double r;
    }Circle;
    
    Shape* makeCircle(double r);
    
    #endif

    circle.c

    #include <assert.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include "shape.h"
    #include "circle.h"
    
    const double PI = 3.14159;
    
    static void area(void *shape)
    {
        Circle *_circle = (Circle*)((Shape *)shape)->shapeData;
        printf("the circle area is %f 
    ", _circle->r * _circle->r * PI);
    }
    
    Shape* makeCircle(double r)
    {
        Shape *shape = (Shape *)malloc(sizeof(Shape));
        Circle *circle = (Circle *)malloc(sizeof(Circle));
        assert(shape != NULL && circle != NULL);
        assert(r > 0);
    
        circle->r = r;
        shape->shapeData = circle;
        shape->area = &area;
        shape->release = &release;
    
        return shape;
    }

    rectange.h

    #ifndef RECTANGLE_H
    #define RECTANGLE_H
    
    typedef struct{
        float x;
        float y;
    }Rectangle;
    
    
    Shape *makeRectangle(float x, float y);
    #endif

    rectange.c

    #include <assert.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include "shape.h"
    #include "rectangle.h"
    
    static void area(void *shape)
    {
        Rectangle *rectangle = (Rectangle*)((Shape *)shape)->shapeData;
        printf("the rectangle area is %f 
    ", rectangle->x * rectangle->y);
    }
    
    Shape* makeRectangle(float x, float y)
    {
        Shape *shape = (Shape *)malloc(sizeof(Shape));
        Rectangle *rectangle = (Rectangle *)malloc(sizeof(Rectangle));
        assert(shape != NULL && rectangle != NULL);
        assert(x > 0 && y > 0);
    
        rectangle->x = x;
        rectangle->y = y;
        shape->shapeData = rectangle;
        shape->area = &area;
        shape->release = &release;
    
        return shape;
    }

    main.c

    #include <stdio.h>
    #include "shape.h"
    #include "circle.h"
    #include "rectangle.h"
    
    void printShapeArea(Shape **shapes,int length)
    {
        int i=0;
        for(i=0;i<length;i++)
        {
            Shape *shape = shapes[i];
            shape->area(shape);
            shape->release(shape);
        }
    }
    
    int main()
    {
        Shape *p[3] = {makeCircle(3.2),makeCircle(3.2),makeRectangle(3,4)};
        printShapeArea(p,3);
        return 0;
    }
  • 相关阅读:
    QT 中如何实现一个简单的动画
    qt 中画线时如何设置笔的颜色和填充
    QT自定义窗口
    qt 中创建一个工作线程(例子)
    QT 获取系统时间
    火狐浏览器 system error code 1722 rpc服务器不可用和谷歌浏览器的插件application/x-print-ladop不支持
    ORA-10858:在要求输入数字处找到非数字字符
    eaeyui-combobox实现组合查询(即实现多个值得搜索)
    Mybatis中的模糊查询
    如何设置像我这样的博客的样式。
  • 原文地址:https://www.cnblogs.com/code-style/p/3219415.html
Copyright © 2011-2022 走看看