[C] How to Use C to practice OOP

Source: Internet
Author: User

Because of work needs, it is now necessary to use C to develop the program of the ingress-type system.

To enhance the skill, let's take a look at how to use C to practice oop.

This example is based on xcode3.2.3 + gcc4.2 ......

This is a little different from the examples found by Google .....

Http://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c

 

# Include <stdio. h> <br/> # include <stdlib. h> <br/> typedef void (* pai_fun) (struct animal * a_this); <br/> typedef struct animal * (* dtor_fun) (struct animal * a_this ); <br/> typedef struct _ animal_vtable {<br/> pai_fun walk; <br/> dtor_fun dtor; <br/>} animal_vtable; <br/> typedef struct _ animal {<br/> animal_vtable vtable; </P> <p> char * Name; <br/>} animal; <br/> typedef struct _ dog {<br/> animal_vtable vtable; </P> <p> char * Name; // mirror member variables for easy access <br/> char * type; <br/>}dog; <br/> void animal_walk (animal * a_this) {<br/> printf ("Animal (% s) walking/N", a_this-> name ); <br/>}< br/> struct animal * animal_dtor (struct animal * a_this) {<br/> printf ("Animal: dtor/N "); <br/> return a_this; <br/>}< br/> animal * animal_alloc () {<br/> return (animal *) malloc (sizeof (animal )); <br/>}< br/> animal * animal_new (animal * a_animal) {<br/> a_animal-> vtable. walk = animal_walk; <br/> a_animal-> vtable. dtor = animal_dtor; <br/> a_animal-> name = "anonymous"; <br/> return a_animal; <br/>}< br/> void animal_free (animal * a_this) {<br/> a_this-> vtable. dtor (a_this); </P> <p> free (a_this); <br/>}< br/> void dog_walk (dog * a_this) {<br/> printf ("dog walking % s (% s)/n", a_this-> type, a_this-> name ); <br/>}< br/> dog * dog_dtor (dog * a_this) {<br/> // explicit call to parent destructor <br/> animal_dtor (animal *) a_this); </P> <p> printf ("Dog: dtor/N"); </P> <p> return a_this; <br/>}< br/> dog * dog_alloc () {<br/> return (dog *) malloc (sizeof (DOG )); <br/>}< br/> dog * dog_new (dog * a_dog) {<br/> // explict call to parent constructor <br/> animal_new (animal *) a_dog); </P> <p> a_dog-> type = "dog type"; <br/> a_dog-> vtable. walk = (pai_fun) dog_walk; <br/> a_dog-> vtable. dtor = (dtor_fun) dog_dtor; </P> <p> return a_dog; <br/>}</P> <p> int main (INT argc, const char * argv []) {<br/>/* <br/> base class: <br/> animal * a_animal = animal_new (animal_alloc ()); <br/> */<br/> animal * a_animal = (animal *) dog_new (dog_alloc (); </P> <p> a_animal-> vtable. walk (a_animal); </P> <p> animal_free (a_animal); </P> <p> return 0; <br/>}< br/>

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.