Previous: http://www.bkjia.com/kf/201301/184226.html
An interface abstracts behaviors and can be used to implement class polymorphism, imeas. h defines an interface for measuring the circumference and area: [cpp] # 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 thi The s pointer is declared as void * type, which facilitates pointer conversion. Class has attributes and methods, and the polymorphism method achieves the goal by implementing the interface, crect. h/crect. c defines and implements a rectangle class: [cpp] # 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 _ */[cpp] # 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 */} ing of methods when constructing a constructor, the implementation here is the method in the interface imeas, 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 the circular class: [cpp] # 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 _ */[cpp] # 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;} 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 */} circular class implements the imeas Interface And your diam method. Note that you can directly map your method.