/*- * Copyright (c) 2012 valsorym . * All rights reserved. * * Discussion on the forums.freebsd.org. * url: http://forums.freebsd.org/showthread.php?p=177208 */ /* * Very simple class, for example OOD in ANSI C. */ #include #include #include #include "employee.h" /* INTERFACES */ /* Private: * For the experiment, the method of Private. */ static int goodage(int ); /* Public: */ /* Constructor & Destructor */ static struct w_employee *create(int , char *, char *); static void destroy(struct w_employee *); static char *setpost(struct w_employee *, char *); static char *getpost(struct w_employee *); /* w_human methods. */ static int setage(struct w_employee *, int ); static int getage(struct w_employee *); static char *setname(struct w_employee *, char *); static char *getname(struct w_employee *); /* Initialization (constructor / destructor). */ const struct w_employee_vtable w_employee = { .create = create, .destroy = destroy, .setpost = setpost, .getpost = getpost, .setage = setage, .getage = getage, .setname = setname, .getname = getname }; /* IMPLEMENTATIONS */ /* Private: */ /* * In this class, the method gooage() determines whether the normal * age of a person. */ static int goodage(int age) { /* * Under the legislation an employee must be between the ages of * 18 to 60 years. */ if (age >= 18 && age <= 60) return 1; return 0; } /* Public: */ /* * The constructor, allocates memory to accommodate the class, * initialize its elements. */ static struct w_employee *create(int age, char *name, char *post) { struct w_employee *self = (struct w_employee *)malloc(sizeof(struct w_employee)); /* Initialize the elements. */ /* Private data. */ if (goodage(age) == 0) age = 18; self->human = w_human.create(age, name); w_employee.setpost(self, post); return self; } /* * The destructor frees the allocated memory in self class. */ static void destroy(struct w_employee *self) { if (self) { if (self->human) w_human.destroy(self->human); if (self->post) free(self->post); free(self); } } /* * Set the post of employee. */ static char *setpost(struct w_employee *self, char *post) { int post_len = strlen(post) + 1; if (self) { if (self->post) free(self->post); self->post = (char *)malloc(post_len * sizeof(char)); strncpy(self->post, post, post_len); return self->post; } return NULL; } /* * Get the post of employee. */ static char *getpost(struct w_employee *self) { if (self) return self->post; return NULL; } /* * Set a person's age. */ static int setage(struct w_employee *self, int age) { if (self && self->human) { if (goodage(age) == 0) /* Use function of private sector. */ return 0; w_human.setage(self->human, age); return w_human.getage(self->human); } return 0; } /* * Get the person's age. */ static int getage(struct w_employee *self) { if (self && self->human) return w_human.getage(self->human); return 0; } /* * Set the name of the person. */ static char *setname(struct w_employee *self, char *name) { if (self && self->human) { w_human.setname(self->human, name); return w_human.getname(self->human); } return NULL; } /* * Get the name of the person. */ static char *getname(struct w_employee *self) { if (self && self->human) return w_human.getname(self->human); return NULL; } /* The End. */