Object-Oriented Programming with C language (3)

Source: Internet
Author: User

How to deal with inheritance in the object-oriented model? Let's take a look at the specific description of the square class: [cpp] # ifndef _ CSQUA_H _ # define _ CSQUA_H _ # include "crect. h "/* Square Class, inherits from Rectangle, for describing square objects */class (csqua) {extends (crect);/* Inherits from crect class */}; # endif/* _ CSQUA_H _ */csqua. in h, the csqua class is inherited directly from the crect class (extends). Square is a special case of the rectangle with the same width and height. [Cpp] # include "csqua. h "static double peri (void * this) {return 4 * (crect *) this)-> width;} static double area (void * this) {return (crect *) this)-> width * (crect *) this)-> width;} constructor (csqua) {mapping (crect. imeas. peri, peri); mapping (crect. imeas. area, area);} destructor (csqua) {return 1;/* Returns 1 for freeing the memory */} csqua. note in c that width is the attribute of the rectangle, so this pointer is actually of the crect * type. So far, the OOSM macro package and a simple and complete example have been introduced. From here, we can see that object-oriented is an idea, not a language patent. Of course, because the c language is process-oriented, it cannot implement boundaries such as private, public, and protect, nor can it hide the this pointer, resulting in the form of c-> diam (c) this is not a natural way of writing, and the inheritance should not be too deep.

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.