Advertisement
Guest User

employee.c

a guest
May 21st, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.89 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 "employee.h"
  18.  
  19. /* INTERFACES */
  20. /* Private:
  21.  * For the experiment, the method of Private.
  22.  */
  23. static int goodage(int );
  24.  
  25. /* Public: */
  26. /* Constructor & Destructor */
  27. static struct w_employee *create(int , char *, char *);
  28. static void destroy(struct w_employee *);
  29.  
  30. static char *setpost(struct w_employee *, char *);
  31. static char *getpost(struct w_employee *);
  32.  
  33. /* w_human methods. */
  34. static int  setage(struct w_employee *, int );
  35. static int  getage(struct w_employee *);
  36. static char *setname(struct w_employee *, char *);
  37. static char *getname(struct w_employee *);
  38.  
  39. /* Initialization (constructor / destructor). */
  40. const struct w_employee_vtable w_employee = {
  41.     .create = create,   .destroy = destroy,
  42.     .setpost = setpost, .getpost = getpost,
  43.    
  44.     .setage  = setage,  .getage  = getage,
  45.     .setname = setname, .getname = getname
  46. };
  47.  
  48. /* IMPLEMENTATIONS */
  49. /* Private: */
  50. /*
  51.  * In this class, the method gooage() determines whether the normal
  52.  * age of a person.
  53.  */
  54. static int
  55. goodage(int age)
  56. {
  57.     /*
  58.      * Under the legislation an employee must be between the ages of
  59.      * 18 to 60 years.
  60.      */
  61.     if (age >= 18 && age <= 60)
  62.         return 1;
  63.        
  64.     return 0;
  65. }
  66.  
  67. /* Public: */
  68. /*
  69.  * The constructor, allocates memory to accommodate the class,
  70.  * initialize its elements.
  71.  */
  72. static struct w_employee
  73. *create(int age, char *name, char *post)
  74. {
  75.     struct w_employee *self =
  76.         (struct w_employee *)malloc(sizeof(struct w_employee));
  77.    
  78.     /* Initialize the elements. */    
  79.     /* Private data. */
  80.     if (goodage(age) == 0)
  81.         age = 18;
  82.  
  83.     self->human = w_human.create(age, name);
  84.     w_employee.setpost(self, post);
  85.  
  86.     return self;
  87. }
  88.  
  89. /*
  90.  * The destructor frees the allocated memory in self class.
  91.  */
  92. static void
  93. destroy(struct w_employee *self)
  94. {
  95.     if (self) {
  96.         if (self->human)
  97.             w_human.destroy(self->human);
  98.  
  99.         if (self->post)
  100.             free(self->post);
  101.  
  102.         free(self);
  103.     }
  104. }
  105.  
  106. /*
  107.  * Set the post of employee.
  108.  */
  109. static char
  110. *setpost(struct w_employee *self, char *post)
  111. {
  112.     int post_len = strlen(post) + 1;
  113.  
  114.     if (self) {
  115.         if (self->post)
  116.             free(self->post);
  117.            
  118.         self->post = (char *)malloc(post_len * sizeof(char));
  119.         strncpy(self->post, post, post_len);
  120.        
  121.         return self->post;
  122.     }
  123.    
  124.     return NULL;
  125. }
  126.  
  127. /*
  128.  * Get the post of employee.
  129.  */
  130. static char
  131. *getpost(struct w_employee *self)
  132. {
  133.     if (self)
  134.         return self->post;
  135.        
  136.     return NULL;
  137. }
  138.  
  139. /*
  140.  * Set a person's age.
  141.  */
  142. static int
  143. setage(struct w_employee *self, int age)
  144. {
  145.     if (self && self->human) {
  146.         if (goodage(age) == 0) /* Use function of private sector. */
  147.             return 0;
  148.        
  149.         w_human.setage(self->human, age);
  150.         return w_human.getage(self->human);
  151.     }
  152.  
  153.     return 0;
  154. }
  155.  
  156. /*
  157.  * Get the person's age.
  158.  */
  159. static int
  160. getage(struct w_employee *self)
  161. {
  162.     if (self && self->human)
  163.         return w_human.getage(self->human);
  164.        
  165.     return 0;
  166. }
  167.  
  168. /*
  169.  * Set the name of the person.
  170.  */
  171. static char
  172. *setname(struct w_employee *self, char *name)
  173. {
  174.     if (self && self->human) {
  175.         w_human.setname(self->human, name);
  176.         return w_human.getname(self->human);
  177.     }
  178.    
  179.     return NULL;
  180. }
  181.  
  182. /*
  183.  * Get the name of the person.
  184.  */
  185. static char
  186. *getname(struct w_employee *self)
  187. {
  188.     if (self && self->human)
  189.         return w_human.getname(self->human);
  190.        
  191.     return NULL;
  192. }
  193.  
  194. /* The End. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement