Encapsulation of data and methods in C Language

Source: Internet
Author: User

In C, struct can be used to replace classes, and function pointers can be used to replace Member methods to encapsulate data members and Member methods. The program written on the client is similar to C ++, the only difference is that the address of this object must be passed to the function when the function pointer member is called in C, because the function status in C is the same.

This article imitates the Vector class in STL to write a vector structure in C language. The program is as follows:

1. Vector Interface

/*************************************** * **************************** Created: created: 19: 8: 2013 0: 09 file base: vectorfile Ext: hauthor: justme0 (http://blog.csdn.net/Justme0) Purpose: definition of vector struct ************************************ * *******************************/# ifndef _ vector_h _# define _ vector_h_typedef struct vector; typedef charvec_value_type; typedef vec_value_type * vec_pointer; typedef vec_value_type * vec_iterator; typedef unsigned struct; struct vector {/*** obtain the element whose subscript is Index */vec_value_type (* get_at) (vector * PVEC, const int index);/*** set the subscript to ELEM */void (* set_at) (vector * PVEC, const int index, const vec_value_type ELEM); vec_iterator (* begin) (vector * PVEC); vec_iterator (* End) (vector * PVEC); vec_value_type (* Front) (vector * PVEC ); vec_value_type (* back) (vector * PVEC); int (* size) (vector * PVEC); int (* Capacity) (vector * PVEC); int (* empty) (vector * PVEC); void (* insert_n) (vector * PVEC, const vec_iterator position, const vec_size_type N, const vec_value_type ELEM); vec_iterator (* earse_pos) (vector * PVEC, const vec_iterator position); vec_iterator (* earse_int) (vector * PVEC, const vec_iterator first, const vec_iterator last); void (* clear) (vector * PVEC); void (* push_back) (vector * PVEC, const vec_value_type ELEM); void (* pop_back) (vector * PVEC); vec_iterator _ start; vec_iterator _ finish; vec_iterator _ end_of_storage ;}; void vec_construct (vector * PVEC); void vec_construct_n (vector * PVEC, const int size); void vec_destruct (vector * PVEC); # endif

2. Implementation of Vector

/*************************************** * **************************** Created: created: 19: 8: 2013 0: 09 file base: vectorfile Ext: cauthor: justme0 (http://blog.csdn.net/Justme0) Purpose: implementation of vector ************************************* * ******************************/# include "vector. H "# include <math. h> # include <stdlib. h> # include <assert. h> # define check_border assert (PVEC-> _ finish> = PVEC-> _ start & & PVEC-> _ end_of_storage> = PVEC-> _ start) Static vec_iterator copy (vec_iterator first, vec_iterator last, vec_iterator result) {vec_iterator src = first; vec_iterator DST = result; for (; SRC! = Last; ++ SRC, ++ DST) {* DST = * SRC;} return DST;} static vec_value_type _ get_at (vector * PVEC, int index) {return * (PVEC-> begin (PVEC) + index);} static void _ set_at (vector * PVEC, int index, vec_value_type ELEM) {PVEC-> _ start [Index] = ELEM;} static vec_iterator _ begin (vector * PVEC) {return PVEC-> _ start;} static vec_iterator _ end (vector * PVEC) {return PVEC-> _ finish;} static vec_value_type _ Front (vector * PVE C) {return * PVEC-> begin (PVEC);} static vec_value_type _ back (vector * PVEC) {return * (PVEC-> end (PVEC)-1 );} static int _ SIZE (vector * PVEC) {return PVEC-> end (PVEC)-PVEC-> begin (PVEC);} static int _ capacity (vector * PVEC) {return PVEC-> _ end_of_storage-PVEC-> begin (PVEC);} static int _ Empty (vector * PVEC) {return PVEC-> begin (PVEC) = PVEC-> end (PVEC);} static void _ insert_n (vector * PVEC, vec_iterator positi On, vec_size_type N, const vec_value_type ELEM) {vec_size_type old_size = 0; vec_size_type new_size = 0; int inset_index = 0; vec_iterator ite = NULL; assert (PVEC-> _ start <= Position & position <= PVEC-> end (PVEC); check_border; If (0 = N) {return ;} inset_index = position-PVEC-> _ start; old_size = PVEC-> size (PVEC); new_size = old_size + N; // check whether the remaining space is sufficient, if not, expand if (vec_size_type) (PVEC-> _ end_of_storage- PVEC-> _ finish) <n) {const vec_size_type new_capacity = old_size + _ max (old_size, n); vec_value_type * new_base = (vec_value_type *) realloc (PVEC-> _ start, new_capacity * sizeof (vec_value_type); If (null = new_base) {exit (overflow ); // Memory leakage will occur in the original space} PVEC-> _ start = new_base; PVEC-> _ end_of_storage = PVEC-> _ start + new_capacity ;} PVEC-> _ finish = PVEC-> _ start + new_size; position = PVEC-> _ start + inset_ind Ex; // move the element for (ITE = PVEC-> _ finish; ite> = Position + N; -- ITE) {* ite = * (ITE-N );} // insert n new elements for (; ite >=position; -- ITE) {* ite = ELEM ;}} static vec_iterator _ earse_pos (vector * PVEC, const vec_iterator position) {If (Position + 1! = PVEC-> end (PVEC) {copy (Position + 1, PVEC-> _ finish, position) ;}-- PVEC-> _ finish; return position ;} static vec_iterator _ earse_int (vector * PVEC, const vec_iterator first, const vec_iterator last) {vec_iterator I = copy (last, PVEC-> _ finish, first ); PVEC-> _ finish-= last-first; return first;} static void _ clear (vector * PVEC) {PVEC-> earse_int (PVEC, PVEC-> begin (PVEC ), PVEC-> end (PVEC);} static void _ push_back (vector * PVEC, const vec_value_type ELEM) {check_border; _ insert_n (PVEC, PVEC-> end (PVEC ), 1, ELEM);} static void _ pop_back (vector * PVEC) {PVEC-> earse_pos (PVEC, PVEC-> end (PVEC)-1 );} static void set (vector * PVEC) {PVEC-> _ finish = NULL; PVEC-> _ start = NULL; PVEC-> _ end_of_storage = NULL; PVEC-> get_at = _ get_at; PVEC-> set_at = _ set_at; PVEC-> begin = _ begin; PVEC-> end = _ end; PVEC-> front = _ front; PVEC-> back = _ back; PVEC-> size = _ size; PVEC-> capacity = _ capacity; PVEC-> Empty = _ Empty; PVEC-> insert_n = _ insert_n; PVEC-> earse_pos = _ earse_pos; PVEC-> earse_int = _ earse_int; PVEC-> clear = _ clear; PVEC-> push_back = _ push_back; PVEC-> pop_back = _ pop_back;} static void reset (vector * PVEC) {PVEC-> _ finish = NULL; PVEC-> _ start = NULL; PVEC-> _ end_of_storage = NULL; PVEC-> get_at = NULL; PVEC-> set_at = NULL; PVEC-> begin = NULL; PVEC-> end = NULL; PVEC-> front = NULL; PVEC-> back = NULL; PVEC-> size = NULL; PVEC-> capacity = NULL; PVEC-> Empty = NULL; PVEC-> insert_n = NULL; PVEC-> earse_pos = NULL; PVEC-> earse_int = NULL; PVEC-> clear = NULL; PVEC-> push_back = NULL; PVEC-> pop_back = NULL;} void vec_construct (vector * PVEC) {set (PVEC);} void vec_construct_n (vector * PVEC, const int size) {set (PVEC ); PVEC-> _ start = (vec_iterator) malloc (size * sizeof (* PVEC-> _ start); If (null = PVEC-> _ start) {// todo: exit (overflow);} PVEC-> _ finish = PVEC-> _ start + size; PVEC-> _ end_of_storage = PVEC-> _ finish;} void vec_destruct (vector * PVEC) {free (PVEC-> _ start); reset (PVEC );}

3. Test Procedure

/*************************************** * **************************** Created: created: 19: 8: 2013 0: 10 file base: testfile Ext: cauthor: justme0 (http://blog.csdn.net/Justme0) Purpose: vector testing program ************************************ * *******************************/# include "vector. H "# include <stdio. h> void output (vector * PVEC) {vec_iterator ITER; For (iter = PVEC-> begin (PVEC); iter! = PVEC-> end (PVEC); ++ ITER) {printf ("% C \ n", * ITER) ;}} int main (INT argc, char ** argv) {char CH = 'a'; int CNT = 5; vector my_vec; vec_construct (& my_vec); While (CNT --) {my_vec.push_back (& my_vec, CH ++ );} output (& my_vec); puts ("set [2]: '2'"); my_vec.set_at (& my_vec, 2, '2'); output (& my_vec ); my_vec.empty (& my_vec )? Puts ("empty"): puts ("not empty"); puts ("pop_back... "); my_vec.pop_back (& my_vec); output (& my_vec); printf (" size is % d \ n ", my_vec.size (& my_vec )); printf ("back is '% C' \ n", my_vec.back (& my_vec); puts ("clear... "); my_vec.clear (& my_vec); my_vec.empty (& my_vec )? Puts ("empty"): puts ("not empty"); vec_destruct (& my_vec); Return 0 ;}

4. Running result

Abcdeset [2]: '2' ab2denot emptypop_back... ab2dsize is 4 back is 'D' clear... empty press any key to continue...

1. In the test program, we can see that after defining a struct, we must use the Construct Function to assign values to the members of the object for initialization. I call this process a "constructor ".

2. the destruct function must be called to "analyze" the object and release the space of the object malloc.

I showed this program to a C ++ game programmer and was approved by him for a while, saying that the biggest drawback of my program is that it is not object-oriented; no company will let this program pass; "you are writing objective-C format ". Sang Xin, I had to stick it here to enjoy it alone.

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.