Created
October 25, 2014 11:12
-
-
Save ixn/b5bcca5aa0fbac159540 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); | |
| } |
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
| CC=gcc | |
| CFLAGS=-I. | |
| LDFLAGS= | |
| FUNGSIOBJ = object.c\ | |
| fungsiobj.c | |
| all:object | |
| object: $(FUNGSIOBJ) | |
| $(CC) $(CFLAGS) -o object $^ $(LDFLAGS) | |
| clean: | |
| rm -f *~ | |
| rm -f *.o | |
| rm -f object |
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" | |
| int | |
| main(int argc, char* argv[]) | |
| { | |
| /* Deklarasi obj sebagai object */ | |
| object* obj; | |
| /* Melakukan pengalokasian memory pada obj */ | |
| obj = object_new(); | |
| /* Deklarasi variable object 1 & object 2 */ | |
| char object1[1024]; | |
| char object2[1024]; | |
| printf("Masukkan nilai object 1 = "); | |
| scanf("%s",object1); | |
| // Mengisi nilai object 1 pada char array, scanf tidak bisa mengisi langsung ke pointer. | |
| // jad digunakan char array untuk mengisi nilai object 1 dari scanf | |
| printf("Masukkan nilai object 2 = "); | |
| scanf("%s",object2); | |
| // Mengisi nilai object 2 pada char array, scanf tidak bisa mengisi langsung ke pointer. | |
| // jadi digunakan char array untuk mengisi nilai object 2 dari scanf | |
| // Mengisikan obj->object1 yang berasal dari nilai object1 | |
| obj->object1=object1; | |
| // Mengisikan obj->object2 yang berasal dari nilai object2 | |
| obj->object2=object2; | |
| //Memanggil fungsi display untuk melakukan printf | |
| object_display(obj); | |
| //Memanggil fungsi penghapusan memory yang digunakan oleh obj | |
| object_free(obj); | |
| return 0; | |
| } |
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
| #ifndef __OBJECT_H__ | |
| #define __OBJECT_H__ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| /* membuat pengelompokan data dengan struct | |
| kita beri nama object yang berisi (object1 & object2) | |
| */ | |
| struct object | |
| { | |
| char* object1; | |
| char* object2; | |
| }; | |
| /* struct object di inisialkan dengan nama yang sama yaitu "object" | |
| jadi saat pendeklarasian menggunakan/mengikutkan struct object, | |
| tinggal menggunakan object* | |
| */ | |
| typedef struct object object; | |
| /* Deklarasi object */ | |
| /* object_new dibuat untuk mengalokasinya memory pada struct object. | |
| harus melakukan object_free() saat menggunakan fungsi ini | |
| */ | |
| object* | |
| object_new(void); | |
| /* object_free untuk menghapus memory yang dialokasikan oleh object_new() */ | |
| void | |
| object_free(object* obj); | |
| /* fungsi ini untuk melakukan display/printf data. | |
| data yang masuk bersumber dari program utama (main) | |
| */ | |
| void | |
| object_display(object* obj); | |
| #endif //__OBJECT_H__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment