参考资料:

     http://blog.chinaunix.net/uid-26750235-id-3102371.html

     http://www.eventhelix.com/realtimemantra/basics/ComparingCPPAndCPerformance2.htm#.U0jCR7KBR7h

 

说明:

         Shape类为基类,包含typeName,虚函数为求解Shape的周长、面积、析构函数

 

头文件 Cobject.h

#pragma once

/*
* 使用C实现面向对象编程:封装、继承、多态
*/ //基类
struct Shape
{
char *typeName;
struct ShapeOps *ops;
}; //基类虚函数指针
struct ShapeOps
{
float (*OpsArea)(struct Shape* shape); //求面积
int (*OpsPerimeter)(struct Shape* shape); //求周长
void (*OpsDestory)(struct Shape *shape); //析构对象
}; //基类的虚函数
float Area(struct Shape *shape);
int Perimeter(struct Shape *shape);
void Destory(struct Shape *shape); /*********************************************************************************/ //派生类“三角形”triangle
struct Triangle
{
struct Shape shape;
int a;
int b;
int c;
}; //派生类接口
struct Triangle* TriCreate(int a,int b,int c);
float TriArea(struct Shape *triangle);
int TriPerimeter(struct Shape *triangle);
void TriDestory(struct Shape *triangle); extern struct ShapeOps TriangleOps; /*********************************************************************************/ //派生类“矩形” rectangle
struct Rectangle
{
struct Shape shape;
int w;
int h;
}; //派生类接口
struct Rectangle* RectCreate(int w,int h);
float RectArea(struct Shape *rectangle);
int RectPerimeter(struct Shape *rectangle);
void RectDestory(struct Shape *rectangle); extern struct ShapeOps RectangleOps;

 

源文件 Cobject.c

#include "Cobject.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h> //基类接口
float Area(struct Shape *shape)
{
assert(shape != NULL);
return shape->ops->OpsArea(shape);
} int Perimeter(struct Shape *shape)
{
assert(shape != NULL);
return shape->ops->OpsPerimeter(shape);
} void Destory(struct Shape *shape)
{
assert(shape != NULL);
printf("%s Destory\n",shape->typeName);
shape->ops->OpsDestory(shape);
} //Triangle 具体接口实现
struct Triangle* TriCreate(int a,int b,int c)
{
struct Triangle *tri=(struct Triangle *)(malloc(sizeof(struct Triangle)));
tri->shape.typeName="Triangle";
tri->shape.ops=&TriangleOps;
tri->a=a;
tri->b=b;
tri->c=c; return tri;
} float TriArea(struct Shape *shape)
{
struct Triangle *tri;
assert(shape != NULL);
tri=(struct Triangle *)(shape);
return (float)(tri->a * tri->b * tri->c);
} int TriPerimeter(struct Shape *shape)
{
struct Triangle *tri;
assert( shape != NULL);
tri=(struct Triangle *)shape;
return ( tri->a + tri->b + tri->c)/2;
} void TriDestory(struct Shape *shape)
{
struct Triangle *tri;
assert(shape != NULL);
tri =(struct Triangle *)shape;
free(tri);
} struct ShapeOps TriangleOps={TriArea,TriPerimeter,TriDestory}; //Rectangle 具体接口实现
struct Rectangle *RectCreate(int w,int h)
{
struct Rectangle * rect=(struct Rectangle *)malloc(sizeof(struct Rectangle));
rect->shape.typeName="Rectangle";
rect->shape.ops=&RectangleOps;
rect->w=w;
rect->h=h; return rect;
} float RectArea(struct Shape *shape)
{
struct Rectangle *rect;
assert( shape !=NULL );
rect=(struct Rectangle *)shape;
return (float)(rect->h * rect->w );
} int RectPerimeter(struct Shape *shape)
{
struct Rectangle *rect;
assert(shape != NULL );
rect=(struct Rectangle *)shape;
return 2*(rect->h + rect->w);
} void RectDestory(struct Shape *shape)
{
struct Rectangle *rect;
assert(shape != NULL);
rect=(struct Rectangle *)shape;
free(rect);
} struct ShapeOps RectangleOps={RectArea,RectPerimeter,RectDestory};

 

测试文件 CobjectTest.cpp

extern "C"{
#include "Cobject.h"
};
#include <iostream>
using namespace std; int main()
{
Shape *shape[2]; //创建Triangle
shape[0]=(Shape*)TriCreate(1,3,2); //创建Rectangle
shape[1]=(Shape*)RectCreate(2,3); for(int i=0;i<2;i++)
{
cout<<"Area: "<<Area(shape[i])<<endl;
cout<<"Perimeter: "<<Perimeter(shape[i])<<endl;
Destory(shape[i]);
}
}

 

注意:

       1、C不支持函数重载,以及函数默认参数

       2、头文件中函数名默认为extern

       3、C++调用C函数,使用extern “C”

05-11 07:56