C Implements object-oriented

Source: Internet
Author: User

Object-oriented is simply encapsulation inheritance and polymorphism

Encapsulation: struct IMPLEMENTATION

Inheritance: pointer implementation, which is to include the parent class in the struct.

Polymorphic: can be implemented with pointers.

Generally implemented polymorphism, the parent struct must be the first element of a child struct, so that it can be cast arbitrarily by casting subclasses and parent classes.

Structures such as:

[CPP]View Plaincopy
  1. struct parent{
  2. int A;
  3. };
  4. struct child{
  5. struct parent p;
  6. int b;
  7. };
  8. That's why the following conversions
  9. struct Child *c= (struct Child *) malloc (sizeof (struct-child));
  10. c->p.a=10;
  11. struct Child *c= (struct Child *) & (C->P);
  12. There are many C languages that use the first address to convert the type



And the Linux kernel polymorphism is not the way to achieve

General and Containof () and void *

->: Used to get the parent class

Containof (): Used to get subclasses from parent class

void *: Equivalent to the object class in Java can convert a class to void * and then convert it back elsewhere. Long is used in the parameters of the function.

Such as:

[CPP]View Plaincopy
  1. #include <stdio.h>
  2. struct base{
  3. int A;
  4. Char b;
  5. };
  6. struct subclass{
  7. struct base *mybase;
  8. int s_a;
  9. Char S_b;
  10. };
  11. void Test (struct base *base)
  12. {
  13. //Forces pointer base pointing to struct struct base to pointer to struct subclass sub
  14. //Here is the downward transformation
  15. //struct Subclass *sub= (struct subclass *) base;
  16. struct Subclass *sub=base;
  17. printf ("a:%d\tb:%c\n", sub->mybase->a,sub->mybase->b);
  18. /*some operations*/
  19. }
  20. void Main (void)
  21. {
  22. struct Subclass *mysub= (struct subclass *) malloc (sizeof (mysub));
  23. mysub->mybase= (struct base *) malloc (sizeof (struct base));
  24. mysub->mybase->a=10;
  25. mysub->mybase->b=' A ';
  26. mysub->s_a=11;
  27. mysub->s_b=' B ';
  28. //Here is the upward transformation,
  29. //This is not like Java and C + + can automatically transform upward, all must be manually,
  30. //This does not mean that if you do not convert with an expression, there will be problems .
  31. //Because our class here is not a real class, there will be obvious inheritance in C + + or Java such as A:base
  32. struct base *mybase;
  33. Mybase=mysub;
  34. Test (MyBase);
  35. }

PS: Some other articles

From: http://blog.chinaunix.net/uid-26750235-id-3102371.htmlhttp://blog.chinaunix.net/uid-26750235-id-3102371.html

       In geometry, all geometry types inherit the parent class shape, and the parent class shape has two properties S_type and S_name. Where S_type is used to represent the type to which the shape belongs, S_name is used to name the table in that shape state. And the parent shape has two virtual interfaces, one for Shape_area to return the shape's area, and one for Shape_perimeter to return the perimeter of the shape. Both interfaces must be implemented by subclasses of the child's inherited shape.
    1. Struct shape;
    2. Struct shape_ops
    3. {
    4.     /* returns the area of the geometry */
    5.     float  (*so_area) ( struct shape*);  
    6.     /* Returns the perimeter of the geometry */
    7.     int   (*so_perimeter) (struct shape*);
    8. };
    9. Struct shape
    10. {
    11.     int* s_type;
    12.     Char* s_name;
    13.     struct shape_ops* s_ops; /* Virtual interface, all subclasses must implement */
    14. };
    15. Float shape_area (struct shape* s)  /* shape area */
    16. {
    17.      return s->s_ops->so_area (s);  
    18. }
    19. Int shape_perimeter (struct shape* s)/* for perimeter */
    20. {
    21.  & Nbsp;  return s->s_ops->so_perimeter (s);
    22. }
The geometry "triangle (triangle)" Inherits the parent class "shape" and implements the two virtual interfaces of the parent class. The "triangle" has three sides, respectively, with T_side_a,t_side_b,t_side_c to the length of the three sides.
  1. /* Triangle */
  2. struct triangle
  3. {
  4. struct shape t_base;
  5. int t_side_a;
  6. int t_side_b;
  7. int T_side_c;
  8. };
  9. float Triangle_area (struct shape* s)/* Triangle area, with Helen Formula */
  10. {
  11. struct triangle* t= (struct triangle*) s;
  12. int a=t->t_side_a;
  13. int b=t->t_side_b;
  14. int c=t->t_side_c;
  15. Float p= (a+b+c)/2;
  16. return sqrt (p* (p-a) * (p-b) * (p-c));
  17. }
  18. int Triangle_perimeter (struct shape* s)/* triangle circumference */
  19. {
  20. struct triangle* t= (struct triangle*) s;
  21. int a=t->t_side_a;
  22. int b=t->t_side_b;
  23. int c=t->t_side_c;
  24. return a+b+c;
  25. }
  26. struct Shape_ops triangle_ops=/* Implementation of the parent virtual interface */
  27. {
  28. Triangle_area,
  29. Triangle_perimeter,
  30. };
  31. struct triangle* triangle_create (int a,int b,int c)/* Create triangle */
  32. {
  33. struct triangle* ret= (struct triangle*) malloc (sizeof (*ret));
  34. Ret->t_base.s_name= "triangle";
  35. ret->t_base.s_ops=&triangle_ops;
  36. ret->t_side_a=a;
  37. ret->t_side_b=b;
  38. ret->t_side_c=c;
  39. return ret;
  40. }
The geometry "Rectangle (Rectangle)" Inherits the parent class "shape", which also implements the two virtual interfaces of the parent class. There are two properties R_width and R_height, each representing the length and width of the rectangle.
  1. /* Rectangle */
  2. struct Rectangle
  3. {
  4. struct shape r_base;
  5. int r_width;
  6. int r_height;
  7. };
  8. float Rectangle_area (struct shape* s)/* rectangular area */
  9. {
  10. struct rectangle* r= (struct rectangle*) s;
  11. Return r->r_width*r->r_height;
  12. }
  13. int Rectangle_perimeter (struct shape* s)/* Rectangle circumference */
  14. {
  15. struct rectangle* r= (struct rectangle*) s;
  16. Return (r->r_width+r->r_height) * *;
  17. }
  18. struct Shape_ops rectangle_ops=/* Implementation of the parent virtual interface */
  19. {
  20. Rectangle_area,
  21. Rectangle_perimeter,
  22. };
  23. struct rectangle* rectangle_create (int width, int height)/* Create rectangle */
  24. {
  25. struct rectangle* ret= (struct rectangle*) malloc (sizeof (*ret));
  26. Ret->r_base.s_name= "Rectangle";
  27. ret->r_base.s_ops=&rectangle_ops;
  28. ret->r_height=height;
  29. ret->r_width=width;
  30. return ret;
  31. }
Test code:
  1. int main ()
  2. {
  3. struct shape* s[4];
  4. S[0]=triangle_create (5,5,4);
  5. S[1]=triangle_create (3,4,5);
  6. S[2]=rectangle_create (10,12);
  7. S[3]=rectangle_create (5,8);
  8. int i=0;
  9. for (i=0;i<4;i++)
  10. {
  11. Float Area=shape_area (s[i]);
  12. int Perimeter=shape_perimeter (s[i]);
  13. char* name=s[i]->s_name;
  14. printf ("name:%s, area:%.2f, perimeter:%d\n", name,area,perimeter);
  15. }
  16. return 0;
  17. }
Operation Result:
  1. Name:triangle, area:9.17, perimeter:14
  2. Name:triangle, area:6.00, Perimeter:12
  3. Name:rectangle, area:120.00, perimeter:44
  4. Name:rectangle, area:40.00, perimeter:26

From: http://blog.csdn.net/kennyrose/article/details/7564105

Recently, during the interview process, students were asked such a question: How to achieve object-oriented with C language? We all know the three basic features of object-oriented: encapsulation, inheritance and polymorphism, the C + + language and compilers all have strong support for these features, but how to implement object-oriented for a functional language like C? Quote a WORD: object-oriented is always thought, not language! Understanding object-oriented programming ideas, we use the C language, such as lower-level language can also be implemented OOP, which is specifically used in the C language of the macro, structure, function pointers, aggregation and other knowledge.

It is highly recommended to refer to the following links


Http://blog.codingnow.com/2010/03/object_oriented_programming_in_c.html

Http://c.group.iteye.com/group/wiki/1291-object-oriented-programming-language-c

Http://blog.chinaunix.net/uid-26750235-id-3102371.html

Http://blog.chinaunix.net/uid-9104650-id-2009591.html

Http://liujian.is-programmer.com/posts/268.html

http://blog.csdn.net/yuyin86/article/details/7107671#

Two books recommended:

"Inside the C + + Object Model"

"Object-oriented Programming with Ansi-c"

This is the Chinese translation of Csdn's previous chapters.

http://blog.csdn.net/besidemyself/article/details/6376405

All the Chinese and English translations on the wiki

Http://wiki.chinaunix.net/OOC:%e5%86%85%e5%ae%b9

However, we recommend reading against the original English

Http://www.planetpdf.com/codecuts/pdfs/ooc.pdf

Here are some examples of object-oriented programming with ANSI-C

Http://www.bolthole.com/OO-C-programming.html

Http://barracudaserver.com/WP/DeviceControl/OOIntro.html

Http://www.eventhelix.com/realtimemantra/basics/object_oriented_programming_in_c.htm

Here are some of the OOP design ideas and principles of the article

http://www.eventhelix.com/realtimemantra/Object_Oriented/

The following article gives C + + and corresponding C code, C for the implementation of C + + inheritance and virtual function characteristics, recommended @ ^ @

Http://www.eventhelix.com/realtimemantra/basics/ComparingCPPAndCPerformance2.htm

The following is a lightweight object-oriented C programming framework LW--OOPC(light Weight object-oriented Programming with C)

http://sourceforge.net/projects/lwoopc/

Here's a video tutorial above Youku (I haven't seen it yet)

Http://v.youku.com/v_show/id_XMTM4MzkyMTI4.html

Accidentally found a "C language can do great things."

Http://www.rupeng.com/forum/forum-52-1.html

If you are interested, you can also learn about Objective-c, which is a C-language master that joins object-oriented thinking

Http://zh.wikipedia.org/wiki/Objective-C

Http://www.apple.com.cn/developer/mac/library/documentation/Cocoa/Conceptual/OOP_ObjC/Introduction/chapter_1_ Section_1.html#//apple_ref/doc/uid/tp40005149-ch1-sw2

      • C Language Object-oriented programming (i): Encapsulation and inheritance
      • C Language Object-oriented programming (II): inheritance Explained
      • C Language Object-oriented programming (III): virtual function and polymorphism
      • C Language Object-oriented programming (IV): interface-oriented programming
      • C Language Object-oriented programming (V): single-linked list implementation
      • C Language Object-oriented programming (VI): Configuration file parsing

C Implement object-oriented (GO)

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.