[C] Using C to practice OOP and new and delete

Source: Internet
Author: User

After studying it, I was barely able to integrate it.

Writing C into OOP is quite fun.

Compiling environment: xcode3.2.3 + gcc4.2

 

/* <Br/> * cloud. h <br/> * cloudooc <br/> * created by cloud on 2010/8/21. <br/> * Copyright 2010 orz. all rights reserved. <br/> */<br/> # ifndef object_h <br/> # define object_h <br/> typedef void (* execute) (struct object * a_this); <br/> typedef struct object * (* allocate )(); <br/> typedef struct _ object_vtable <br/>{< br/> char * Name; <br/> execute EXE; <br/>} object_vtable; <br/> typedef struct _ basecmd <br/>{< br/> object_vtable vtable; <br/>} basecmd; <br/> // --- for common --- // <br/> basecmd * New (allocate newobj); <br/> void Delete (void * item ); <br/> // ------------------------ // <br/> // --- for basecmd --- // <br/> basecmd * newbasecmd (); <br/> void exe_basecmd (basecmd * a_this ); <br/> // ------------------------ // <br/> // --- for homecmd --- // <br/> typedef struct _ homecmd <br/> {<br/> object_vtable vtable; <br/>} homecmd; <br/> basecmd * newhomecmd (); <br/> void exe_homecmd (homecmd * a_this ); <br/> // -------------------------- // <br/> # endif

 

/* <Br/> * cloud. c <br/> * cloudooc <br/> * created by cloud on 2010/8/21. <br/> * Copyright 2010 orz. all rights reserved. <br/> */<br/> # include "cloud. H "<br/> // --- for common --- // <br/> basecmd * New (allocate newobj) <br/>{< br/> basecmd * OBJ = newobj (); <br/> return OBJ; <br/>}< br/> void Delete (void * item) <br/>{< br/> free (item ); <br/>}< br/> // ------------------------ // <br/> // --- for basecmd --- // <br/> basecmd * newbasecmd () <br/> {<br/> basecmd * cmd = malloc (sizeof (basecmd); <br/> cmd-> vtable. name = "Cloud"; <br/> cmd-> vtable.exe = exe_basecmd; <br/> return cmd; <br/>}< br/> void exe_basecmd (basecmd * a_this) <br/> {<br/> printf ("Do base command = % s/n", a_this-> vtable. name); <br/>}< br/> // ------------------------ // <br/> // --- for homecmd --- // <br/> basecmd * newhomecmd () <br/>{< br/> homecmd * cmd = malloc (sizeof (homecmd); <br/> cmd-> vtable. name = "home"; <br/> cmd-> vtable.exe = exe_homecmd; <br/> return cmd; <br/>}< br/> void exe_homecmd (homecmd * a_this) <br/> {<br/> printf ("Do home command = % s/n", a_this-> vtable. name); <br/>}< br/> //--------------------------//

 

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include "cloud. H "<br/> int main (INT argc, const char * argv []) <br/>{</P> <p> basecmd * cmd = new (newbasecmd ); <br/> cmd-> vtable.exe (CMD); <br/> Delete (CMD); </P> <p> basecmd * cmd2 = new (newhomecmd ); <br/> cmd2-> vtable.exe (cmd2); <br/> Delete (cmd2); </P> <p> return 0; <br/>}< br/>

 

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.