-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.c
More file actions
52 lines (44 loc) · 1.08 KB
/
objects.c
File metadata and controls
52 lines (44 loc) · 1.08 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
//
// Created by Ethan Sawyer on 7/5/20.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Employee {
char *name;
int id;
};
struct Employee *new_Employee(char *name, int id) {
struct Employee *this = (struct Employee *) malloc(sizeof(struct Employee));
if (this == NULL) {
return NULL; // Out of memory...
}
this->name = strdup(name);
this->id = id;
return this;
}
char *strdup(const char *s) {
char *t = (char *) malloc(strlen(s) + 1);
strcpy(t, s);
return t;
}
char *Employee_get_name(struct Employee *this) {
return this->name;
}
int Employee_get_id(struct Employee *this) {
return this->id;
}
void Employee_print(struct Employee *this) {
printf("Employee[%s, %d]", this->name, this->id);
}
void Employee_free(struct Employee *this) {
free(this->name);
free(this);
}
//int main(int argc, char *argv[]) {
// struct Employee *e = new_Employee("Alan Turing", 123);
// printf("%s\n", Employee_get_name(e));
// printf("%d\n", Employee_get_id(e));
// Employee_print(e);
// Employee_free(e);
//}