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
- struct parent{
- int A;
- };
- struct child{
- struct parent p;
- int b;
- };
- That's why the following conversions
- struct Child *c= (struct Child *) malloc (sizeof (struct-child));
- c->p.a=10;
- struct Child *c= (struct Child *) & (C->P);
- 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
- #include <stdio.h>
- struct base{
- int A;
- Char b;
- };
- struct subclass{
- struct base *mybase;
- int s_a;
- Char S_b;
- };
- void Test (struct base *base)
- {
- //Forces pointer base pointing to struct struct base to pointer to struct subclass sub
- //Here is the downward transformation
- //struct Subclass *sub= (struct subclass *) base;
- struct Subclass *sub=base;
- printf ("a:%d\tb:%c\n", sub->mybase->a,sub->mybase->b);
- /*some operations*/
- }
- void Main (void)
- {
- struct Subclass *mysub= (struct subclass *) malloc (sizeof (mysub));
- mysub->mybase= (struct base *) malloc (sizeof (struct base));
- mysub->mybase->a=10;
- mysub->mybase->b=' A ';
- mysub->s_a=11;
- mysub->s_b=' B ';
- //Here is the upward transformation,
- //This is not like Java and C + + can automatically transform upward, all must be manually,
- //This does not mean that if you do not convert with an expression, there will be problems .
- //Because our class here is not a real class, there will be obvious inheritance in C + + or Java such as A:base
- struct base *mybase;
- Mybase=mysub;
- Test (MyBase);
- }
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.
- Struct shape;
- Struct shape_ops
- {
- /* returns the area of the geometry */
- float (*so_area) ( struct shape*);
- /* Returns the perimeter of the geometry */
- int (*so_perimeter) (struct shape*);
- };
- Struct shape
- {
- int* s_type;
- Char* s_name;
- struct shape_ops* s_ops; /* Virtual interface, all subclasses must implement */
- };
-
- Float shape_area (struct shape* s) /* shape area */
- {
- return s->s_ops->so_area (s);
- }
- Int shape_perimeter (struct shape* s)/* for perimeter */
- {
- & Nbsp; return s->s_ops->so_perimeter (s);
- }
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.
- /* Triangle */
- struct triangle
- {
- struct shape t_base;
- int t_side_a;
- int t_side_b;
- int T_side_c;
- };
- float Triangle_area (struct shape* s)/* Triangle area, with Helen Formula */
- {
- struct triangle* t= (struct triangle*) s;
- int a=t->t_side_a;
- int b=t->t_side_b;
- int c=t->t_side_c;
- Float p= (a+b+c)/2;
- return sqrt (p* (p-a) * (p-b) * (p-c));
- }
- int Triangle_perimeter (struct shape* s)/* triangle circumference */
- {
- struct triangle* t= (struct triangle*) s;
- int a=t->t_side_a;
- int b=t->t_side_b;
- int c=t->t_side_c;
- return a+b+c;
- }
- struct Shape_ops triangle_ops=/* Implementation of the parent virtual interface */
- {
- Triangle_area,
- Triangle_perimeter,
- };
- struct triangle* triangle_create (int a,int b,int c)/* Create triangle */
- {
- struct triangle* ret= (struct triangle*) malloc (sizeof (*ret));
- Ret->t_base.s_name= "triangle";
- ret->t_base.s_ops=&triangle_ops;
- ret->t_side_a=a;
- ret->t_side_b=b;
- ret->t_side_c=c;
- return ret;
- }
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.
- /* Rectangle */
- struct Rectangle
- {
- struct shape r_base;
- int r_width;
- int r_height;
- };
- float Rectangle_area (struct shape* s)/* rectangular area */
- {
- struct rectangle* r= (struct rectangle*) s;
- Return r->r_width*r->r_height;
- }
- int Rectangle_perimeter (struct shape* s)/* Rectangle circumference */
- {
- struct rectangle* r= (struct rectangle*) s;
- Return (r->r_width+r->r_height) * *;
- }
- struct Shape_ops rectangle_ops=/* Implementation of the parent virtual interface */
- {
- Rectangle_area,
- Rectangle_perimeter,
- };
- struct rectangle* rectangle_create (int width, int height)/* Create rectangle */
- {
- struct rectangle* ret= (struct rectangle*) malloc (sizeof (*ret));
- Ret->r_base.s_name= "Rectangle";
- ret->r_base.s_ops=&rectangle_ops;
- ret->r_height=height;
- ret->r_width=width;
- return ret;
- }
Test code:
- int main ()
- {
- struct shape* s[4];
- S[0]=triangle_create (5,5,4);
- S[1]=triangle_create (3,4,5);
- S[2]=rectangle_create (10,12);
- S[3]=rectangle_create (5,8);
- int i=0;
- for (i=0;i<4;i++)
- {
- Float Area=shape_area (s[i]);
- int Perimeter=shape_perimeter (s[i]);
- char* name=s[i]->s_name;
- printf ("name:%s, area:%.2f, perimeter:%d\n", name,area,perimeter);
- }
- return 0;
- }
Operation Result:
- Name:triangle, area:9.17, perimeter:14
- Name:triangle, area:6.00, Perimeter:12
- Name:rectangle, area:120.00, perimeter:44
- 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)