- #include <stdlib.h>
- #include <unistd.h>
-
-
- typedef struct compteur {
- int nb;
-
- void (*increment)(struct compteur *this);
- void (*decrement)(struct compteur *this);
-
- int (*getValue)(struct compteur *this);
- void (*setValue)(struct compteur *this, int n);
-
- void (*free)(struct compteur *this);
- } compteur;
-
-
- /* définition des différentes méthodes */
- void __compteur__increment(compteur *this){
- this->nb++;
- }
-
- void __compteur__decrement(compteur *this){
- this->nb--;
- }
-
- int __compteur__getValue(compteur *this){
- return this->nb;
- }
-
- void __compteur__setValue(compteur *this, int n){
- this->nb = n;
- }
-
- /* fin des méthodes générales */
-
- /* méthodes particulières */
-
- // destructeur
- void __compteur__free(compteur *this){
- free(this); // libération
- }
-
- // constructeur
- compteur *Compteur() {
- compteur *n = malloc(sizeof(compteur)); // allocation
-
- n->nb = 0;
-
- // set des fonctions
- n->increment = __compteur__increment;
- n->decrement = __compteur__decrement;
- n->getValue = __compteur__getValue;
- n->setValue = __compteur__setValue;
- n->free = __compteur__free;
-
-
- return n;
- }
- /* fin méthodes particulières */
-
- int main(void){
- // création
- compteur *c = Compteur();
-
- // traitement
- c->setValue(c,20);
- c->increment(c);
- c->increment(c);
-
- // affichage
- printf("%d\n",c->getValue(c));
-
- // libération
- c->free(c);
- }
#include <stdlib.h>
#include <unistd.h>
typedef struct compteur {
int nb;
void (*increment)(struct compteur *this);
void (*decrement)(struct compteur *this);
int (*getValue)(struct compteur *this);
void (*setValue)(struct compteur *this, int n);
void (*free)(struct compteur *this);
} compteur;
/* définition des différentes méthodes */
void __compteur__increment(compteur *this){
this->nb++;
}
void __compteur__decrement(compteur *this){
this->nb--;
}
int __compteur__getValue(compteur *this){
return this->nb;
}
void __compteur__setValue(compteur *this, int n){
this->nb = n;
}
/* fin des méthodes générales */
/* méthodes particulières */
// destructeur
void __compteur__free(compteur *this){
free(this); // libération
}
// constructeur
compteur *Compteur() {
compteur *n = malloc(sizeof(compteur)); // allocation
n->nb = 0;
// set des fonctions
n->increment = __compteur__increment;
n->decrement = __compteur__decrement;
n->getValue = __compteur__getValue;
n->setValue = __compteur__setValue;
n->free = __compteur__free;
return n;
}
/* fin méthodes particulières */
int main(void){
// création
compteur *c = Compteur();
// traitement
c->setValue(c,20);
c->increment(c);
c->increment(c);
// affichage
printf("%d\n",c->getValue(c));
// libération
c->free(c);
}