Advertisement
Guest User

human.c

a guest
May 21st, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.85 KB | None | 0 0
  1. /*-
  2.  * Copyright (c) 2012 valsorym <[email protected]>.
  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_vtable w_human = {
  39.     .create  = create,  .destroy = destroy,
  40.     .setage  = setage,  .getage  = getage,
  41.     .setname = setname, .getname = getname
  42. };
  43.  
  44. /* IMPLEMENTATIONS */
  45. /* Private: */
  46. /*
  47.  * In this class, the method gooage() determines whether the normal
  48.  * age of a person.
  49.  */
  50. static int
  51. goodage(int age)
  52. {
  53.     if (age >= 0 && age <= 120)
  54.         return 1;
  55.        
  56.     return 0;
  57. }
  58.  
  59. /* Public: */
  60. /*
  61.  * The constructor, allocates memory to accommodate the class,
  62.  * initialize its elements.
  63.  */
  64. static struct w_human
  65. *create(int age, char *name)
  66. {
  67.     struct w_human *self = (struct w_human *)malloc(sizeof(struct w_human));
  68.    
  69.     /* Private data. */
  70.     w_human.setage(self, age);
  71.     w_human.setname(self, name);
  72.  
  73.     return self;
  74. }
  75.  
  76. /*
  77.  * The destructor frees the allocated memory in self class.
  78.  */
  79. static void
  80. destroy(struct w_human *self)
  81. {
  82.     if (self) {
  83.         if (self->name)
  84.             free(self->name);
  85.  
  86.         free(self);
  87.     }
  88. }
  89.  
  90. /*
  91.  * Set a person's age.
  92.  */
  93. static int
  94. setage(struct w_human *self, int age)
  95. {
  96.     if (self) {
  97.         if (goodage(age) == 0) /* Use function of private sector. */
  98.             return 0;
  99.        
  100.         self->age = age;
  101.     }
  102.  
  103.     return self->age;
  104. }
  105.  
  106. /*
  107.  * Get the person's age.
  108.  */
  109. static int
  110. getage(struct w_human *self)
  111. {
  112.     if (self)
  113.         return self->age;
  114.        
  115.     return 0;
  116. }
  117.  
  118. /*
  119.  * Set the name of the person.
  120.  */
  121. static char
  122. *setname(struct w_human *self, char *name)
  123. {
  124.     int name_len = strlen(name) + 1;
  125.  
  126.     if (self) {
  127.         if (self->name)
  128.             free(self->name);
  129.            
  130.         self->name = (char *)malloc(name_len * sizeof(char));
  131.         strncpy(self->name, name, name_len);
  132.        
  133.         return self->name;
  134.     }
  135.    
  136.     return NULL;
  137. }
  138.  
  139. /*
  140.  * Get the name of the person.
  141.  */
  142. static char
  143. *getname(struct w_human *self)
  144. {
  145.     if (self)
  146.         return self->name;
  147.        
  148.     return NULL;
  149. }
  150.  
  151. /* The End. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement