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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include <stdio.h>
#include <stdlib.h>
typedef struct Point__ Point_s;
struct Point__ {
double x;
double y;
};
/* -tc- constructeur: permet de creer un objet de type Point_s */
Point_s *point_creer(double x, double y)
{
Point_s *p_self = NULL;
/* -tc- allocation de la memoire necessaire pour un point */
p_self = malloc(sizeof *p_self);
if (p_self != NULL)
{
static Point_s tmp = {0.0, 0.0};
*p_self = tmp;
/* -tc- initialisation des donnees membres */
p_self->x = x;
p_self->y = y;
}
return p_self;
}
/* -tc- destructeur: permet de detruire un objet de type Point_s */
void point_detruire(Point_s **const pp_self)
{
if (pp_self != NULL && *pp_self != NULL)
{
free(*pp_self), *pp_self = NULL;
}
}
void point_deplacer(Point_s *const p_self, double delta_x, double delta_y)
{
if (p_self != NULL)
{
p_self->x += delta_x;
p_self->y += delta_y;
}
}
/* -tc- affiche un objet de type Point_s */
void point_afficher(Point_s const *const p_self)
{
if (p_self != NULL)
{
printf("Point(%.2f, %.2f)\n", p_self->x, p_self->y);
}
}
int main(void)
{
Point_s *p = point_creer(10.2, 22.3);
point_afficher(p);
point_deplacer(p, 5, 5);
point_afficher(p);
point_detruire(&p);
return 0;
} |
Partager