0804-c Language and design patterns (inheritance, encapsulation, polymorphism)

Source: Internet
Author: User
Tags pprint

C + + has three of the most important features, namely inheritance, encapsulation, polymorphism. I found that in fact, C language can be object-oriented, but also can apply design patterns, the key is how to implement the object-oriented language of the three important attributes.

( 1 ) of inheritance

1. typedef struct _parent

2. {

3. int data_parent;

4.

5. }parent;

6.

7. typedef struct _child

8. {

9. struct _parent parent;

data_child int ;

One by one .

}child;

When designing C-language inheritance, all we need to do is put the underlying data in the first place of the inherited structure. In this way, there is no problem with data access, strong data transfer, and data access.

( 2 ) Encapsulation of

1. struct _data;

2.

3. typedef void (*process) (struct _data* pData);

4.

5. typedef struct _data

6. {

7. int value;

8. process pprocess;

9.

}data;

The implication of encapsulation is that functions and data are tied together, and data and data are tied together. In this way, we can access all the data through a simple structure pointer, traversing all the functions. Encapsulation, which is the property owned by the class, and of course the property of the data structure body.


( 3 ) polymorphic

1. typedef struct _play

2. {

3. void* PDATA;

4. void (*start_play) (struct _play* pplay);

5. }play;

Polymorphic, which means that different data is processed using the same interface code. For example, the play structure here is a common data structure, and we don't know what data pdata is, what is the Start_play function? However, we can only call Pplay->start_play (Pplay) When we are dealing with it. The rest of the things we do not need to tube, because different interfaces will have different functions to deal with, we just have to learn to call it.


#include <stdio.h> #include <stdlib.h>struct cmd{char *p;void (*prun) (struct cmd *pcmd); void (*pprint) ( struct cmd *pcmd);}; typedef  struct cmd  cmd;void run (cmd *pcmd) {System (PCMD->P);} void print (CMD *pcmd) {printf ("%s", pcmd->p);} struct newcmd{struct cmd cmd1;int (*plength) (struct newcmd *pnewcmd);}; int getlength (struct newcmd *pnewcmd) {return strlen (PNEWCMD->CMD1.P);//Return length}void main () {struct newcmd newcmd1; NEWCMD1.CMD1.P = "Notepad"; newcmd1.cmd1.pprint = Print;newcmd1.cmd1.prun = Run;newcmd1.plength = getlength;// Initialize Newcmd1.cmd1.pprint (&NEWCMD1.CMD1), Newcmd1.cmd1.prun (&NEWCMD1.CMD1);p rintf ("%d", Newcmd1.plength ( &NEWCMD1)); System ("Pause");} void Main1 () {CMD cmd1 = {"Notepad", run, print};cmd1.pprint (&CMD1); Cmd1.prun (&CMD1); System ("pause");

#include <stdio.h> #include <stdlib.h>struct ren{void (*pj) (struct ren *p); struct Nanren{struct ren ren1;void (*pj) (struct Nanren *p);}; void Nanrenya (struct Nanren *pnan) {printf ("Man's Tiger Tooth");} struct Nvren{struct ren ren1;void (*pj) (struct Nvren *p);}; void Nvrenya (struct Nvren *pnv) {printf ("Woman's Baiya");} void Gettooth (struct ren *pren) {printf ("Gogogogo");} void Main () {struct Nanren  TANSHENG1;TANSHENG1.REN1.PJ = GETTOOTH;TANSHENG1.REN1.PJ (&TANSHENG1.REN1); TANSHENG1.PJ = NANRENYA;TANSHENG1.PJ (&tansheng1); struct Nvren   ftansheng1;ftansheng1.ren1.pj = Gettooth; FTANSHENG1.REN1.PJ (&FTANSHENG1.REN1); FTANSHENG1.PJ = NVRENYA;FTANSHENG1.PJ (&ftansheng1); GetChar ();}


Copyright notice: This blog all articles are original, welcome to Exchange, welcome reprint, reprint do not tamper with the content, and indicate the source, thank you!

0804-c Language and design patterns (inheritance, encapsulation, polymorphism)

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.