Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Person Person;
- struct Person {
- char* (*get_name)(Person*);
- void (*set_name)(Person*, char* name);
- void (*talk)(Person*);
- char* name_;
- };
- char* Person_get_name(Person* this) {
- return this->name_;
- }
- void Person_set_name(Person* this, char* name) {
- this->name_ = name;
- }
- void Person_talk(Person* this) {
- printf("Hello, this is %s!\n", this->name_);
- }
- Person* new_Person(char* name) {
- Person* this = malloc(sizeof(Person));
- this->get_name = Person_get_name;
- this->set_name = Person_set_name;
- this->talk = Person_talk;
- this->set_name(this, name);
- return this;
- }
- void free_Person(Person* this) {
- free(this);
- }
- int main(void) {
- Person* person = new_Person("Foo");
- person->talk(person);
- free_Person(person);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement