-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemployee.c
More file actions
55 lines (45 loc) · 1.05 KB
/
employee.c
File metadata and controls
55 lines (45 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// Created by Ethan Sawyer on 7/6/20.
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "employee.h"
// This function is in the C standard library but so what
char *strdup(const char *s) {
char *t = (char *) malloc(strlen(s) + 1);
strcpy(t, s);
return t;
}
struct Employee {
char *name;
int id;
};
Employee new_Employee(char *name, int id) {
Employee this = (Employee) malloc(sizeof(struct Employee));
if (this == NULL) {
return NULL; // Out of memory...
}
this->name = strdup(name); // See text
this->id = id;
return this;
}
void Employee_print(Employee this) {
printf("Employee[%s,%d]", this->name, this->id);
}
char *Employee_get_name(Employee this) {
return strdup(this->name);
}
void Employee_set_name(Employee this, char *name) {
this->name = name; // See text
}
int Employee_get_id(Employee this) {
return this->id;
}
void Employee_set_id(Employee this, int id) {
this->id = id;
}
void Employee_free(struct Employee *this) {
free(this->name);
free(this);
}