/* Rectangle.c Class implementation for rectangles */ /* Import some "standard" classes */ #include #include #include "Rectangle.h" /* Import the specification of this class - this enables the compiler to check that the specification and this implementation are consistent */ /* Definition of the attributes of the class */ struct rectangle { double height; double width; }; /**** Methods for operating on the class ****/ /* Constructor */ Rectangle ConsRectangle( double height, double width ) { Rectangle r; r = malloc( sizeof(struct rectangle) ); if ( r == NULL ) { printf("ConsRectangle: Insufficient memory\n"); } else { r->height = height; r->width = width; } return r; } /* Destructor */ void DestroyRectangle( Rectangle r ) { free( r ); } /* Obtain the current height */ double Height( Rectangle r ) { return r->height; } /* Change the height */ void SetHeight( Rectangle r, double height ) { r->height = height; } /* Obtain the current width */ double Width( Rectangle r ) { return r->width; } /* Change the width */ void SetWidth( Rectangle r, double width ) { r->width = width; } /* Properties of rectangles */ double Perimeter( Rectangle r ) { return 2*r->width + 2*r->height; } double Area( Rectangle r ) { return r->width*r->height; }