当前位置: 代码迷 >> C语言 >> [讨论]OO in C
  详细解决方案

[讨论]OO in C

热度:515   发布时间:2005-02-24 11:18:00.0

C语言的结构体struct到底和C++的类Class有什么不同? 结构体是一群相关数据的集合,而类不仅仅是相关数据的集合,更是相关函数的集合。 怎么理解?就是说结构体只能调用结构体包含的变量,而类能调用类包含的方法(函数)。 如果说在结构体中实现了调用相关函数的话,那么我们就说OOP IN C了。

#ifndef class #define class struct #endif

/*-----student类-BEGIN----*/ class student{ class student *this; char name[30]; int math; int chinese; int english; void(*sum)(class student*); void(*ave)(class student*); void(*list)(class student*); }; /*-----方法池-BEGIN----*/ void sum_method(class student*p){ printf("SUM=%d\n",p->math+p->chinese+p->english); } void ave_method(class student*p){ printf("AVE=%d\n",(p->math+p->chinese+p->english)/3); } void list_method(class student*p){ printf("%s\n%d\n%d\n%d\n",p->name,p->math,p->chinese,p->english); } /*-----方法池-END-----*/ /*-----类操作-BEGIN----*/ void creatStudent(class student*p,char *n,int m,int c,int e){ p->this=p; strcpy(p->name,n); p->math=m; p->chinese=c; p->english=e; p->sum=sum_method; p->ave=ave_method; p->list=list_method; } /*-----类操作-END----*/ /*-----student类-END----*/

void main(){ /*-----student类的实例化-BEGIN----*/ class student knocker; class student vlinux; class student live41; creatStudent(&knocker,"Knocker",50,60,70); creatStudent(&vlinux,"vlinux",95,98,90); creatStudent(&live41,"live41",80,90,100); /*-----student类的实例化-END----*/ /*-----这里,三个实例分别调用了三个结构体所包含的方方法(函数)-----*/ (*knocker.list)(&knocker); (*knocker.sum)(&knocker);

(*vlinux.list)(&vlinux); (*vlinux.ave)(&vlinux);

(*live41.list)(&live41); getch(); }


----------------解决方案--------------------------------------------------------
嗯,此贴值一个8位的QQ
----------------解决方案--------------------------------------------------------
oo 是C语言编出来的,C++是用C语言编写,用C语言实现当然是可行的。
----------------解决方案--------------------------------------------------------
楼上的错了,这不是真正的OO,我看你的问题还是在学的是C++,C#,却没有一点OO的思想.
----------------解决方案--------------------------------------------------------
OOP是一种思想,你不能刻意去描述的。往往你越是刻意,就显得越牵强。

万法自然

一种思想是任何一种语言都可以描述的。
这就像是‘相对论’,不管英语也好、法语也好,他们都可以最其进行精确的描述。

[此贴子已经被作者于2005-2-27 0:01:00编辑过]



----------------解决方案--------------------------------------------------------
顶起来,不要沉了
----------------解决方案--------------------------------------------------------
OOP是一种思想,你不能刻意去描述的。往往你越是刻意,就显得越牵强。

万法自然

同意,顶一下!!!
----------------解决方案--------------------------------------------------------
以下是引用神vLinux飘飘在2005-2-26 23:59:26的发言: OOP是一种思想,你不能刻意去描述的。往往你越是刻意,就显得越牵强。 万法自然 一种思想是任何一种语言都可以描述的。 这就像是‘相对论’,不管英语也好、法语也好,他们都可以最其进行精确的描述。
支持. 现在的开发语言, 不外乎这两种方式, 从某些意义上说, 只是设计思路的不同, 采用那种语言来实现, 不是关键问题.
----------------解决方案--------------------------------------------------------
  相关解决方案