• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C实现面向对象封装、继承、多态

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

参考资料:

     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”


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
分分钟用上C#中的委托和事件发布时间:2022-07-13
下一篇:
C#——性能计数器发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap