I would not for OOP and OOP, it is some of the characteristics of OOP , such as encapsulation , polymorphism is actually software engineering ideas , these ideas , regardless of language , follow these ideas can make the program more flexible and easier to modify and maintain avoid rigid, fragile
shape.h This file defines the graphical interface must implement all the specific graphics themselves calculate the area , perimeter and other API
#ifndef SHAPE_H
#define SHAPE_H
typedef struct shape_t
{
void *shapeData;
void (*area)(void *);
void (*release)(void *);
}Shape;
#_endif
circle.h Round Interface
#ifndef CIRCLE_H
#define CIRCLE_H
typedef struct
{
double r;
}CircleData;
typedef struct
{
void *shapeData;
void (*area)(void *);
void (*release)(void *);
}Circle;
Circle *makeCircle(double r);
#_endif
circle.c Round implementation code , static decorated function is the equivalent private function
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "circle.h"
const double PI = 3.14159;
static void area(void *shape)
{
Circle *_circle = (Circle *)shape;
CircleData* data = (CircleData*)_circle->shapeData;
printf("the circle area is %f \n", data->r * data->r * PI);
}
static void release(void *shape)
{
Circle *_circle = (Circle *)shape;
CircleData* data = (CircleData*)_circle->shapeData;
free(data);
free(_circle);
}
Circle *makeCircle(double r)
{
CircleData* circleData = (CircleData*)malloc(sizeof(CircleData));
Circle* circle = (Circle*)malloc(sizeof(Circle));
assert(circleData != NULL);
assert(circle != NULL);
assert(r > 0);
circleData->r = r;
circle->shapeData = circleData;
circle->area = &area;
circle->release = &release;
return circle;
}
square.h
#ifndef SQUARE_H
#define SQUARE_H
typedef struct
{
double x;
double y;
}SquareData;
typedef struct
{
void *shapeData;
void (*area)(void *);
void (*release)(void *);
}Square;
Square *makeSquare(double x, double y);
#_endif
square.c
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "square.h"
static void area(void *shape)
{
Square *square = (Square *)shape;
SquareData* data = (SquareData*)square->shapeData;
printf("the square area is %f \n", data->x * data->y);
}
static void release(void *shape)
{
Square *square = (Square *)shape;
SquareData* data = (SquareData*)square->shapeData;
free(data);
free(square);
}
Square *makeSquare(double x, double y)
{
SquareData* squareData = (SquareData*)malloc(sizeof(SquareData));
Square* square = (Square*)malloc(sizeof(Square));
assert(squareData != NULL);
assert(square != NULL);
assert(x > 0 && y > 0);
squareData->x = x;
squareData->y = y;
square->shapeData = squareData;
square->area = &area;
square->release = &release;
return square;
}
main.c all the work , all for it , is the code to make it stable
#include <stdio.h>
#include "shape.h"
#include "circle.h"
#include "square.h"
void printShapeArea(Shape **shape,int length)
{
int i=0;
for(i=0;i<length;i++)
{
shape[i]->area(shape[i]);
shape[i]->release(shape[i]);
}
}
int main()
{
Shape *p[3] = {(Shape*)makeCircle(3.2),(Shape*)makeCircle(3.2),(Shape*)makeSquare(3.1,4)};
printShapeArea(p,3);
return 0;
}
Summary :
printShapeArea function does not know the incoming graphic list , respectively, which are the graphics, which are how they calculate the area and perimeter, it is only known that these graphical interface to calculate the area and perimeter of what is , through this interface is calculated like
没有评论:
发表评论