Advertisement
Guest User

employee.c

a guest
May 20th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 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. /* Initialization (constructor / destructor). */
  34. const struct w_employee_initializer w_employee = {
  35.     .create = create, .destroy = destroy
  36. };
  37.  
  38. /* IMPLEMENTATIONS */
  39. /* Private: */
  40. /*
  41.  * In this class, the method gooage() determines whether the normal
  42.  * age of a person.
  43.  */
  44. static int
  45. goodage(int age)
  46. {
  47.     /*
  48.      * Under the legislation an employee must be between the ages of
  49.      * 18 to 60 years.
  50.      */
  51.     if (age >= 18 && age <= 60)
  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_employee
  63. *create(int age, char *name, char *post)
  64. {
  65.     struct w_employee *self =
  66.         (struct w_employee *)malloc(sizeof(struct w_employee));
  67.    
  68.     /* Initialize the elements. */
  69.     /* Methods. */
  70.     self->setpost = setpost; self->getpost = getpost;
  71.    
  72.     /* Private data. */
  73.     if (goodage(age) == 0)
  74.         age = 18;
  75.  
  76.     self->human = w_human.create(age, name);
  77.     self->setpost(self, post);
  78.  
  79.     return self;
  80. }
  81.  
  82. /*
  83.  * The destructor frees the allocated memory in self class.
  84.  */
  85. static void
  86. destroy(struct w_employee *self)
  87. {
  88.     if (self) {
  89.         if (self->human)
  90.             w_human.destroy(self->human);
  91.  
  92.         if (self->post)
  93.             free(self->post);
  94.  
  95.         free(self);
  96.     }
  97. }
  98.  
  99. /*
  100.  * Set the post of employee.
  101.  */
  102. static char
  103. *setpost(struct w_employee *self, char *post)
  104. {
  105.     int post_len = strlen(post) + 1;
  106.  
  107.     if (self) {
  108.         if (self->post)
  109.             free(self->post);
  110.            
  111.         self->post = (char *)malloc(post_len * sizeof(char));
  112.         strncpy(self->post, post, post_len);
  113.        
  114.         return self->post;
  115.     }
  116.    
  117.     return NULL;
  118. }
  119.  
  120. /*
  121.  * Get the post of employee.
  122.  */
  123. static char
  124. *getpost(struct w_employee *self)
  125. {
  126.     if (self)
  127.         return self->post;
  128.        
  129.     return NULL;
  130. }
  131.  
  132. /* The End. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement