易截截图软件、单文件、免安装、纯绿色、仅160KB

C 语言的面向对象

C 语言的面向对象
如前所说,面向对象是一种软件设计的思想,是语言无关的。在本节中,我举一个链表(list) 的例子来说明如何在 C 语言中的设计出有面向对象风格的代码。
定义接口
接口是面向对象语言中的一个比较重要的概念,接口只对外部承诺实现该接口的实体可以完成什么样的功能,但是不暴露实现的方式。这样的好处是,实现者可以在不接触接口使用者的代码的情况下,对实现进行调整。
我们来看看链表的接口定义:
清单 1. 链表的接口定义
#ifndef _ILIST_H
  #define           _ILIST_H
   // 定义链表中的节点结构
  typedef struct node{
    void *data;
    struct node *next;
  }Node;
   // 定义链表结构
  typedef struct list{
    struct list *_this;
    Node *head;
    int size;
    void (*insert)(void *node);// 函数指针
    void (*drop)(void *node);
    void (*clear)();
    int (*getSize)();
    void* (*get)(int index);
    void (*print)();
  }List;
   void insert(void *node);
  void drop(void *node);
  void clear();
  int getSize();
  void* get(int index);
  void print();
   #endif   /* _ILIST_H */
  IList 接口中,可以清晰的看到,对于一个 list 实体 ( 也就是对象 ) 来说,可以在其上 进行 insert, drop, clear, getSize, get(index) 以及 print 等操作。
接口的实现
清单 2. 构造方法
  Node *node = NULL;
  List *list = NULL;
   void insert(void *node);
  void drop(void *node);
  void clear();
  int getSize();
  void print();
  void* get(int index);
   List *ListConstruction(){
    list = (List*)malloc(sizeof(List));
    node = (Node*)malloc(sizeof(Node));
    list->head = node;
    list->insert = insert;// 将 insert 函数实现注册在 li


相关文档:

使用Visual C 加速键和菜单

菜单和加速键密切地联系在一起。这两种资源类型联手协作,让用户能更容易地完成任务。大家都知道菜单是什么——它是分层命令结构的物理表示。加速键提供该结构的快捷方式,以提高用户操作的速度。例如,要创建一个新文件,通常用File | New(文件|新建)命令或CTRL-N加速键,两种方法可以产生相同的结果。
Visua ......

C: 面向对象(3)

以下代码演示如何用C来模拟多态。gcc版本:3.4.4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#ifndef class
        #define class struct
#endif
#ifndef private
        ......

[ASM/C/C++]c语言中static 用法总结

假设在test.h中定义了一个static bool g_test=false;
若test1.c和test2.c都包含test.h,则test1.c和test2.c分别生成两份g_test,在test1.c 中置g_test=true,而test2.c中仍然为false并未改变!shit!!
一、c程序存储空间布局
C程序一直由下列部分组成:
      1)正文段——CPU执行 ......

C调用对话框

#include "windows.h"
void main()
{
MessageBox(NULL, "你好啊!", "提示", MB_OK);
return 0;

}

通过windows.h文件调用windows自带的api函数MessageBox,来完成提示框,效果棒
null表示附属的窗体为空
MB_OK表示对话框的类型 ......

C/C++函数调用约定


关于 C/C++ 函数调用约定,大多数时候并不会影响程序逻辑,但遇到跨语言编程时,了解一下还是有好处的。
VC 中默认调用是 __cdecl 方式,Windows API 使用 __stdcall 调用方式,在 DLL 导出函数中,为了跟 Windows API 保持一致,建议使用 __stdcall 方式。
调用约定跟堆栈清除密切相关。如果写一个汇编函数,给 C/C++ ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号