爱因斯坦谜题的c语言解答
JCOOP入门指南[02]

JCOOP入门指南[01]

pingf posted @ Thu, 08 Jul 2010 07:39:44 -1100 in C语言 with tags 创作 , 3440 readers

写一点关于JCOOP的入门指南,JCOOP最核心的部分是由一堆宏构成的,

能够简化C语言描述对象的过程,具体代码见sourceforge.net/projects/jcoop

下面是最简单的一个例子,

涉及到用JCOOP构造类的方法之一以及构造函数与析构函数的使用

因为这个例子非常简单,多的就不写了,直接看注释就好

[那个jc_oop.h要到上面的网址下载,另外最好用GCC来编译,MS那套东西我没测试过,Mingw肯定欧科的]

 
#include <stdio.h>                                                                                                     
#include "./include/jc_oop.h"

static void cry(){
	printf("baby is crying...\n");
}


CLASS(Baby)
	int age; //表示年龄的成员
	METHOD(void,cry)(void); //两个void分别对应返回值和参数
END_CLASS

CTOR(Baby) //构造函式
	printf("Baby constructor called ... \n");
	ASSIGN(age,1); //赋值
	CONNECT(cry,cry); //链接到指定的函式
END_CTOR

DTOR(Baby) //析构函式
	printf("Baby destructor called ... \n");
	UNASSIGN(age); //置零
	DISCONNECT(cry); //断开链接
END_DTOR

int main(){
	NEW(Baby,baby,NULL);
	printf("the baby's age is %d\n",baby->age);    
	baby->cry();
	DEL(Baby,baby);
	return 0;
}

运行时会打印如下的字符串

Baby constructor called ... 
the baby's age is 1
baby is crying...
Baby destructor called ... 

需要注意的是上面的Baby类是构造在堆上的,所以baby自然是一个指针

其在NEW时分配相应的内存,并初始化相应的值(构造函数),

在DEL时调用析构函数释放对应的内存.

从上面的代码不难发现,使用JCOOP不但可以形象的描述一个类,

而且简化了代码的编写,特别是涉及内存分配相关的部分.

 

 

 

AAA said:
Sat, 14 May 2022 06:10:23 -1100

Good site! I really love how it is easy on my eyes and the data are well written. I am wondering how I might be notified whenever a new post has been made. I’ve subscribed to your RSS feed which must do the trick! Have a great day! Selling Payment Processing Services

 

======================================

 

Nice post , this is my first time i read this blog and i found it very interesting , nice job . North American Bancard Agent Program

AAA said:
Mon, 16 May 2022 09:44:02 -1100

Cheers for this particular tips I has been researching all Search engines to be able to come across it! 生物精靈

meidir said:
Thu, 21 Jul 2022 01:53:24 -1100

Your blog has the same post as another author but i like your better. LED補光燈

meidir said:
Wed, 24 Aug 2022 00:26:57 -1100

Appreciate it for all your efforts that you have put in this. very interesting info . skin

meidir said:
Wed, 24 Aug 2022 04:34:40 -1100

We've sell some products of different custom boxes.it is very useful and suprisingly low price please visits this site thanks and please share this post together with your friends. 안마야

MPBSE Board 11th Pre said:
Wed, 24 Aug 2022 17:44:37 -1100

MP Board 11th Class Important Model Question Paper 2023 PDF will be Announced on the Official Website. Madhya Pradesh Class 11th Arts, Science, Commerce Group Important Model Question Paper 2023 Format of Last Year Important Model Question Paper 2023 and Other Attachments are Provided Under. MPBSE Board 11th Previous Paper 2023 MP Board 11th Class Biology, Physics, Economics, Fine Arts, Hindi, English, Second Language, Psychology, Sociology, Political Science and Other Subjects Important Model Question Paper 2023 will be Proclaimed in the Online Mode. Madhya Pradesh Class 11th Standard Pursuing Students can Download the Important Model Question Paper 2023 for the Additional References. on This Page, We Have Given More Details About the MP Board 11th Class Important Model Question Paper 2023, and Exam Format.


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter