Object-Oriented Programming with C language (1)

Source: Internet
Author: User

Many of my friends know that the C language can be used to implement object-oriented programming, but the specific details of the operation are a bit confused. Therefore, based on the study of LW_OOPC, the author makes full simplification of LW_OOPC. It only retains the most basic object-oriented functions and forms its own OOSM macro package. In fact, these things are enough, the source code of the OOSM macro package is as follows: [cpp]/* Object-Oriented Support Macros (OOSM) OOSM is an object-oriented support macros, it makes the C programming language with object-oriented programming capabilities. LW_OOPC ( http://sourceforge.net/projects/lwoopc/ ) Means Light-weight Object-Oriented Programming in C, written by MISOO team, it is a very outstanding works! OOSM inherits the essence of LW_OOPC and crops to retain the basic features. OOSM written by Turingo Studio, but anyone can copies modifies, and distributes it freely. hansome Sun (hansome.sun@gmail.com) January 18,201 3 */# ifndef _ OOSM_H _ # define _ OOSM_H _ # include <stdlib. h> # define class (type) \ typedef struct type; \ type * type ##_ new (void); \ void type ##_ constructor (type * t ); \ int typ E ##_ destructor (type * t); \ void type ##_ delete (type * t); \ struct type # define interface (type) \ typedef struct type; \ struct type # define extends (type) type # define implements (type) type # define constructor (type) \ type * type ##_ new (void) \ {\ type * this; \ this = (type *) malloc (sizeof (type); \ if (this = NULL) return NULL; \ type ##_ constructor (this); \ return this ;\}\\ void ty Pe ### constructor (type * this) # define destructor (type) \ void type ##_ delete (type * this) \{\ if (type ##_ destructor (this) free (this) ;\}\\ int type ##_ destructor (type * this) # define mapping (tf, sf) this-> tf = sf # endif/* _ OOSM_H _ */less than 30 lines of real code, you can intuitively realize that the cost of implementing object-oriented in C language is very low. Supported by the OOSM macro package, the C language code can be so elegant and compact: [cpp] # include <stdio. h> # include "imeas. h "# include" ccirc. h "# include" crect. h "# include" csqua. h "int main (int argc, char * argv []) {ccirc * c = ccirc_new (); crect * r = crect_new (); csqua * s = csqua_new (); c-> radius = 9.23; r-> width = 23.09; r-> height = 78.54; s-> crect. width = 200.00; printf ("Circle: (% lf, % lf, % lf) \ n", c-> diam (c), c-> imeas. peri (c), c-> imeas. area (c )); Printf ("Rectangle: (% lf, % lf) \ n", r-> imeas. peri (r), r-> imeas. area (r); printf ("Square: (% lf, % lf) \ n", s-> crect. imeas. peri (s), s-> crect. imeas. area (s); ccirc_delete (c); crect_delete (r); csqua_delete (s); return 0 ;}so How do ccirc/crect/csqua and other objects describe, see the next set decomposition.

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.