This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

human.c

By: a guest on May 20th, 2012  |  syntax: C  |  size: 2.93 KB  |  views: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*-
  2.  * Copyright (c) 2012 valsorym <valsorym.e@gmail.com>.
  3.  * All rights reserved.
  4.  *
  5.  * Discussion on the forums.freebsd.org.
  6.  *  url: http://forums.freebsd.org/showthread.php?p=177208
  7.  */
  8.  
  9. /*
  10.  * Very simple class, for example OOD in ANSI C.
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <memory.h>
  16.  
  17. #include "human.h"
  18.  
  19. /* INTERFACES */
  20.  
  21. /* Private:
  22.  * For the experiment, the method of Private.
  23.  */
  24. static int goodage(int );
  25.  
  26. /* Public: */
  27. /* Constructor & Destructor */
  28. static struct w_human *create(int , char *);
  29. static void destroy(struct w_human *);
  30.  
  31. /* To access the data, we use the methods. */
  32. static int  setage(struct w_human *, int );
  33. static int  getage(struct w_human *);
  34. static char *setname(struct w_human *, char *);
  35. static char *getname(struct w_human *);
  36.  
  37. /* Initialization (constructor / destructor). */
  38. const struct w_human_initializer w_human = {
  39.     .create = create, .destroy = destroy
  40. };
  41.  
  42. /* IMPLEMENTATIONS */
  43. /* Private: */
  44. /*
  45.  * In this class, the method gooage() determines whether the normal
  46.  * age of a person.
  47.  */
  48. static int
  49. goodage(int age)
  50. {
  51.     if (age >= 0 && age <= 120)
  52.         return 1;
  53.        
  54.     return 0;
  55. }
  56.  
  57. /* Public: */
  58. /*
  59.  * The constructor, allocates memory to accommodate the class,
  60.  * initialize its elements.
  61.  */
  62. static struct w_human
  63. *create(int age, char *name)
  64. {
  65.     struct w_human *self = (struct w_human *)malloc(sizeof(struct w_human));
  66.    
  67.     /* Initialize the elements. */
  68.     /* Methods. */
  69.     self->setage = setage; self->getage = getage;
  70.     self->setname = setname; self->getname = getname;
  71.    
  72.     /* Private data. */
  73.     self->setage(self, age);
  74.     self->setname(self, name);
  75.  
  76.     return self;
  77. }
  78.  
  79. /*
  80.  * The destructor frees the allocated memory in self class.
  81.  */
  82. static void
  83. destroy(struct w_human *self)
  84. {
  85.     if (self) {
  86.         if (self->name)
  87.             free(self->name);
  88.  
  89.         free(self);
  90.     }
  91. }
  92.  
  93. /*
  94.  * Set a person's age.
  95.  */
  96. static int
  97. setage(struct w_human *self, int age)
  98. {
  99.     if (self) {
  100.         if (goodage(age) == 0) /* Use function of private sector. */
  101.             return 0;
  102.        
  103.         self->age = age;
  104.     }
  105.  
  106.     return self->age;
  107. }
  108.  
  109. /*
  110.  * Get the person's age.
  111.  */
  112. static int
  113. getage(struct w_human *self)
  114. {
  115.     if (self)
  116.         return self->age;
  117.        
  118.     return 0;
  119. }
  120.  
  121. /*
  122.  * Set the name of the person.
  123.  */
  124. static char
  125. *setname(struct w_human *self, char *name)
  126. {
  127.     int name_len = strlen(name) + 1;
  128.  
  129.     if (self) {
  130.         if (self->name)
  131.             free(self->name);
  132.            
  133.         self->name = (char *)malloc(name_len * sizeof(char));
  134.         strncpy(self->name, name, name_len);
  135.        
  136.         return self->name;
  137.     }
  138.    
  139.     return NULL;
  140. }
  141.  
  142. /*
  143.  * Get the name of the person.
  144.  */
  145. static char
  146. *getname(struct w_human *self)
  147. {
  148.     if (self)
  149.         return self->name;
  150.        
  151.     return NULL;
  152. }
  153.  
  154. /* The End. */
clone this paste RAW Paste Data