The previous set provides an intuitive introduction to oosm macro packages and Their Applications. Let's take a look at the specific descriptions of CCIRC, crect, csqua, and other objects.
An interface abstracts behaviors and can be used to implement class polymorphism. IMEAS. h defines an interface for measuring the circumference and area:
#ifndef __IMEAS_H__#define __IMEAS_H__#include "oosm.h"/* Measuring Interface, for measuring the perimeter and area of objects */interface(imeas){ double (*peri)(void* this); /* for measuring the perimeter of objects */ double (*area)(void* this); /* for measuring the area of objects */};#endif/*__IMEAS_H__*/
Because the interface is abstract, pay special attention to declaring this pointer as the void * type, which makes it easier to convert the pointer.
Classes have attributes and methods, and polymorphism methods use interfaces to achieve their goals. crect. h/crect. c defines and implements a rectangular class:
#ifndef __CRECT_H__#define __CRECT_H__#include "imeas.h"/* Rectangle Class, for describing rectangle objects */class(crect){ implements(imeas); /* Implements imeas interface */ double width; double height;};#endif/*__CRECT_H__*/
#include "crect.h"static double peri(void* this){ return 2 * (((crect*)this)->width + ((crect*)this)->height);}static double area(void* this){ return ((crect*)this)->width * ((crect*)this)->height;}constructor(crect){ mapping(imeas.peri, peri); mapping(imeas.area, area);}destructor(crect){ return 1; /* Returns 1 for freeing the memory */}
The constructor is mainly used for method ing. Here we implement the methods in the IMEAS interface, so pay special attention to writing the ing as IMEAS. peri/IMEAS. area, the crect_new macro function is automatically generated after the construction. In destructor, you can release the internal space and then return 1 to delete itself. After the analysis, the crect_delete macro function is automatically generated.
Similarly, CCIR. h/CCIR. c defines circular classes:
#ifndef __CCIRC_H__#define __CCIRC_H__#include "imeas.h"/* Circle Class, for describing circular objects */class(ccirc){ implements(imeas); /* Implements imeas interface */ double radius; double (*diam)(ccirc* this); /* for measuring the diameter of the circular objects */};#endif/*__CCIRC_H__*/
#include "ccirc.h"#define PI (3.1415926)static double peri(void* this){ return 2 * PI * ((ccirc*)this)->radius;}static double area(void* this){ return PI * ((ccirc*)this)->radius * ((ccirc*)this)->radius;}static double diam(ccirc* this){ return 2 * this->radius;}constructor(ccirc){ mapping(imeas.peri, peri); mapping(imeas.area, area); mapping(diam, diam);}destructor(ccirc){ return 1; /* Returns 1 for freeing the memory */}
On the basis of implementing the IMEAS interface, the circular class also has its own diam method. Pay attention to the direct ing of its own method.