Created
July 21, 2012 15:00
-
-
Save geovanisouza92/3156064 to your computer and use it in GitHub Desktop.
"Classe" em C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Created on: 21/07/2012 | |
* Author: geovani | |
*/ | |
#include "stdlib.h" | |
#include "stdio.h" | |
typedef struct PessoaBO Pessoa; // Definido antes para facilitar declaração | |
struct PessoaBO | |
{ | |
char * Nome; | |
int Idade; | |
/** Destrutor */ | |
void (*dispose)(Pessoa * self); | |
/** Método envelhecer */ | |
void (*envelhecer)(Pessoa *); | |
}; | |
void Pessoa_dispose (Pessoa * self) | |
{ | |
free (self); | |
} | |
void Pessoa_envelhecer (Pessoa * self) // Implementação do método envelhecer | |
{ | |
self->Idade ++; | |
} | |
Pessoa * Pessoa_new (char * nome, int idade) // Implementação do .ctor | |
{ | |
Pessoa * result = (Pessoa *) malloc (sizeof (Pessoa)); | |
result->dispose = Pessoa_dispose; | |
result->envelhecer = Pessoa_envelhecer; | |
result->Nome = nome; | |
result->Idade = idade; | |
return result; | |
} | |
int main () | |
{ | |
Pessoa * p = Pessoa_new ("geovani\0", 20); | |
p->envelhecer (p); | |
printf ("%s tem %d anos de idade", p->Nome, p->Idade); | |
p->dispose (p); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
É possível inclusive, declarar a struct e os protótipos das funções em um arquivo .h e distribuí-lo por #include...