Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-
- * Copyright (c) 2012 valsorym <[email protected]>.
- * 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 <stdio.h>
- #include <string.h>
- #include <memory.h>
- #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 *);
- /* Initialization (constructor / destructor). */
- const struct w_employee_initializer w_employee = {
- .create = create, .destroy = destroy
- };
- /* 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. */
- /* Methods. */
- self->setpost = setpost; self->getpost = getpost;
- /* Private data. */
- if (goodage(age) == 0)
- age = 18;
- self->human = w_human.create(age, name);
- self->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;
- }
- /* The End. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement