How to use the object-oriented C language

Source: Internet
Author: User

As we all know, C + + is an object-oriented language, but can c use object-oriented functionality?

(1) Inheritance
 struct   _parent  2  { 3  int   data_parent;  4  }parent;  5  typedef struct   _child  6  7  struct   _parent parent;  8   Data_child;  9 }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

Class members are private by default, and the members of a struct are public (cannot be changed), so how do you let the C language implement encapsulated functionality? The answer is function pointers, which are widely used in the kernel.

1 struct _data; 2 typedef  void (*process) (struct _data* pData); 3 struct _data 4 {5     int value; 6     process pprocess; 7 }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) Polymorphism

In C + +, polymorphism is usually implemented using virtual functions, but there is no virtual function in C, how to implement overloading?

The answer is also obvious and is also an extension of the function pointer, taking the following example:

1#include <stdio.h>2#include <stdlib.h>3 4 //virtual function Table structure5 structBase_vtbl6 {7     void(*dance) (void*);8     void(*jump) (void*);9 };Ten  One //base class A struct Base - { -     /*Virtual Table*/ the     structBASE_VTBL *vptr; - }; -  - voidBase_dance (void* This) + { -printf"Base dance\n"); + } A  at voidBase_jump (void* This) - { -printf"Base jump\n"); - } -  - /*Global vtable for base*/ in structBase_vtbl base_table = - { to Base_dance, + Base_jump - }; the  * //constructors for base classes $ struct Base*new_base ()Panax Notoginseng { -     struct Base*temp = (struct Base*)malloc(sizeof(struct Base)); theTemp->vptr = &base_table; +     returntemp; A } the  +  - //Derived Classes $ structderived1 $ { -     struct BaseSuper; -     /*derived members*/ the     intHigh ; - };Wuyi  the voidDerived1_dance (void* This) - { Wu     /*implementation of derived1 ' s dance function*/ -printf"derived1 dance\n"); About } $  - voidDerived1_jump (void* This) - { -     /*implementation of derived1 ' s jump function*/ A     structderived1* temp = (structDERIVED1 *) This; +printf"derived1 jump:%d\n", temp->High ); the } -  $ /*Global vtable for derived1*/ the structBase_vtbl derived1_table = the { the(void(*) (void*)) &Derived1_dance, the(void(*) (void*)) &Derived1_jump - }; in  the //constructors for derived classes the structDerived1 * NEW_DERIVED1 (inth) About { the     structDerived1 * temp= (structDERIVED1 *)malloc(sizeof(structderived1)); theTemp->super.vptr = &derived1_table; theTemp->high =h; +     returntemp; - } the Bayi  the  the intMainvoid) - { -  the     struct Base* bas =new_base (); the     //The member function of the base class is called here theBas->vptr->dance ((void*) bas); theBas->vptr->jump ((void*) bas); -  the  the     structDERIVED1 * Child = new_derived1 ( -); the     //base-class pointers point to derived classes94BAS = (struct Base*) child; the  the     //This call is actually a member function of a derived class. theBas->vptr->dance ((void*) bas);98Bas->vptr->jump ((void*) bas); About     return 0; -}

In summary, we can realize the object-oriented function of C language;

How to use the object-oriented C language

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.