Created
October 25, 2014 11:03
-
-
Save ixn/d0f14248fdc208b3b9ff to your computer and use it in GitHub Desktop.
Simple object oriented in C (http://deeprhezy.tumblr.com)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "object.h" | |
| /* Melakukan pengalokasian memory, menggunakan calloc | |
| Saat menggunakan fungsi ini direkomendasikan untuk | |
| menghapus kembali memory menggunakan object_free() | |
| */ | |
| object* | |
| object_new(void){ | |
| object* obj = (object*)calloc(1, sizeof(object)); | |
| return(obj); | |
| } | |
| /* Melakukan penghapusan data/memory yang dibuat | |
| saat memanggil fungsi object_new() | |
| */ | |
| void | |
| object_free(object* obj){ | |
| /* Saat obj dalam keadaan kosong, | |
| fungsi tidak melakukan apa-apa\ | |
| */ | |
| if(obj == NULL) return; | |
| if(obj->object1 != NULL) obj->object1 = NULL; //menghapus data pada obj->object1 | |
| if(obj->object2 != NULL) obj->object2 = NULL; //menghapus data pada obj->object2 | |
| free(obj); //menghapus fungsi obj | |
| } | |
| void | |
| object_display(object* obj){ | |
| /* Saat obj berisikan NULL, fungsi dikembalikan dan | |
| dan tidak melakukan apa-apa | |
| */ | |
| if(obj == NULL) return; | |
| if(obj->object1 == NULL) return; | |
| if(obj->object2 == NULL) return; | |
| /* Melakukan display/printf data */ | |
| printf("Nilai object 1 = %s \nNilai object 2 = %s \n",obj->object1,obj->object2); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment