Object-Oriented Programming with C language (2)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.