Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Person Person;
  5.  
  6. struct Person {
  7. char* (*get_name)(Person*);
  8. void (*set_name)(Person*, char* name);
  9. void (*talk)(Person*);
  10.  
  11. char* name_;
  12. };
  13.  
  14. char* Person_get_name(Person* this) {
  15. return this->name_;
  16. }
  17.  
  18. void Person_set_name(Person* this, char* name) {
  19. this->name_ = name;
  20. }
  21.  
  22. void Person_talk(Person* this) {
  23. printf("Hello, this is %s!\n", this->name_);
  24. }
  25.  
  26. Person* new_Person(char* name) {
  27. Person* this = malloc(sizeof(Person));
  28. this->get_name = Person_get_name;
  29. this->set_name = Person_set_name;
  30. this->talk = Person_talk;
  31. this->set_name(this, name);
  32. return this;
  33. }
  34.  
  35. void free_Person(Person* this) {
  36. free(this);
  37. }
  38.  
  39. int main(void) {
  40. Person* person = new_Person("Foo");
  41. person->talk(person);
  42. free_Person(person);
  43.  
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement