Structure
In addition to providing basic data types, C also provides users with the ability to customize data types themselves, that is, structures. In C, you can use structures to represent any entity. Structures are the prototype of the concept of classes in object-oriented languages, such as:
Typedef struct{
Float x;
Float y;
}Point;
Defines a point in a plane coordinate system with two fields, the x coordinate and the y coordinate.
The fields in the structure are called members of the structure. The data types in the structure can be simple data types, or other structures, and even the structures themselves can be nested. For example, a standard linked list structure can be defined as follows:
Typedef struct node{
Void *data;// data pointer
Int dataLength;// data length
Struct node *next;// points to the next node
}Node;
As you can see, the type of the next pointer in the structure node is again the node type.
Function pointerThe pointer is the soul of the C language, and it is a more flexible and powerful place for C than other languages. So learning C language must be a good grasp of pointers. A function pointer, a pointer to the first address of a function in a memory map, can be passed as a parameter to another function via a function pointer, and called at the appropriate time to implement asynchronous communication and other functions.
For example, the signal registration function in a UNIX/Linux system has the following prototype:
Void (*signal(int signo,void (*func)(int))) (int)
When using it, you need to define a signal handler externally, and then use signal(sigNo, handler) to register the handler on the process. When the signal occurs, the process can call back the signal handler.
Use function pointers as members of a structure
As mentioned earlier, the members of the structure can be simple data structures, or other structures, and of course, pointers. When a function pointer is used as a member of a structure, and these functions are only used to manipulate data in the structure, an independent entity can be formed. This entity has both data and operations on the data, so that it is natural. Introduce the concept of a class.
Object-oriented language featuresIn general, inheritance, encapsulation, and polymorphism are considered to be three features that an object-oriented language must support. It is through these three characteristics that the object-oriented aspect is better than the process-oriented. Due to the propaganda of language developers or other various reasons, the seemingly object-oriented thinking is realized by language as the carrier. However, in fact, object-oriented is a software design idea, which can be completely independent of the specific implementation. of.
Nonetheless, it is undeniable that these so-called pure object-oriented languages ​​are much better than the process-oriented language in the readability of their code and the matching of human natural thinking.
Language-oriented object-oriented
We generally want to describe an object, generally need to describe some properties of the object, such as a box is an entity, it has 6 faces, with color, weight, whether it is empty and other attributes, and can put things in, you can take Things come out. In an object-oriented language, such an object is usually abstracted into a class:
Class box{
Clolor color;
Int weight;
Boolean empty;
Put(something);
Something get();
}
When you operate the box, you can do something:
Box.put(cake);
Box.get();// Get something from the box.
In a process-oriented language, it is usually passed to a function that runs through the whole world. Similarly, in the case of Box, when you operate on a Box, this is often the case:
Put(Box, cake);// Put a cake in the box
Get(Box);// Take something out of the box
Obviously, the first form of code is more common sense, so object-oriented languages ​​mostly provide this level of language support, making the code more readable and understandable. C language, as a flexible and simple language, we can achieve such a beautiful code form through the simple mechanism provided by C.
Object-oriented in C languageAs mentioned before, object-oriented is a software design idea that is language-independent. In this section, I'll give you an example of a list to illustrate how to design object-oriented style code in C.
Defining interface
An interface is a relatively important concept in an object-oriented language. An interface only performs what functions an entity that implements the interface externally can perform, but does not expose the way it is implemented. This has the advantage that the implementer can adjust the implementation without touching the interface user's code.
Let's take a look at the interface definition of the linked list:
Listing 1. Interface definition for linked lists
#ifndef _ILIST_H
#define _ILIST_H
/ / Define the node structure in the linked list
Typedef struct node{
Void *data;
Struct node *next;
}Node;
/ / Define the linked list structure
Typedef struct list{
Struct list *_this;
Node *head;
Int size;
Void (*insert)(void *node);// function pointer
Void (*drop)(void *node);
Void (*clear)();
Int (*getSize)();
Void* (*get)(int index);
Void (*print)();
}List;
Void insert(void *node);
Void drop(void *node);
Void clear();
Int getSize();
Void* get(int index);
Void print();
#endif /* _ILIST_H */
In the IList interface, you can clearly see that for a list entity (that is, an object), you can perform insert, drop, clear, getSize, get(index), and print operations on it.
Interface implementation
Listing 2. Construction method
Node *node = NULL;
List *list = NULL;
Void insert(void *node);
Void drop(void *node);
Void clear();
Int getSize();
Void print();
Void* get(int index);
List *ListConstruction(){
List = (List*)malloc(sizeof(List));
Node = (Node*)malloc(sizeof(Node));
List->head = node;
List->insert = insert;// Register the insert function implementation on the list entity
List->drop = drop;
List->clear = clear;
List->size = 0;
List->getSize = getSize;
List->get = get;
List->print = print;
List->_this = list;// Save the list itself with the _this pointer
Return (List*)list;
}
Note that the _this pointer here, the _this pointer ensures that the external operation of the list maps to the operation of _this, which simplifies the code.
Listing 3. Insert and delete
// insert a node into a list object
Void insert(void *node){
Node *current = (Node*)malloc(sizeof(Node));
Current->data = node;
Current->next = list->_this->head->next;
List->_this->head->next = current;
(list->_this->size)++;
}
// delete a specified node node
Void drop(void *node){
Node *t = list->_this->head;
Node *d = NULL;
Int i = 0;
For(i;i < list->_this->size;i++){
d = list->_this->head->next;
If(d->data == ((Node*)node)->data){
List->_this->head->next = d->next;
Free(d);
(list->_this->size)--;
Break;
}else{
List->_this->head = list->_this->head->next;
}
}
List->_this->head = t;
}
Other implementation code can be found in the download section, and the space is not listed here.
testTest code
Well, all the work done before is to ensure that our API exposed to the user can be as simple and elegant as possible, now it is time to test:
Listing 4. Test code
Int main(int argc, char** argv) {
List *list = (List*)ListConstruction();// Construct a new linked list
// Insert some values ​​for testing
List->insert("Apple");
List->insert("Borland");
List->insert("Cisco");
List->insert("Dell");
List->insert("Electrolux");
List->insert("FireFox");
List->insert("Google");
List->print();// Print the entire list
Printf("list size = %d",list->getSize());
Node node;
Node.data = "Electrolux";
Node.next = NULL;
List->drop(&node);// delete a node
Node.data = "Cisco";
Node.next = NULL;
List->drop(&node);// delete another node
List->print();// Print again
Printf("list size = %d",list->getSize());
List->clear();// Clear list
Return 0;
}
Figure 1. Results of the operation
ConclusionThe UNIX platform born in C language advocates a design philosophy: try to make simple design, let users connect these simple tools into powerful, complete applications like building blocks. It should be said that C is better to inherit this, C language is very simple and very powerful, and because C language was born earlier, the object-oriented thinking at that time is still immature, so there are a large number of procedural C applications. This gives people the illusion that C language is a process-oriented language. In fact, C only provides some simple, powerful and versatile capabilities. As for what kind of building blocks you want to build, it is up to you.
OVNS Mesh Vape Series is so convenient, portable, and small volume, you just need to take them
out of your pocket and take a puff, feel the cloud of smoke, and the fragrance of fruit surrounding you. It's so great.
We are the distributor of the ovns & vapeak vape brand, we sell ovns disposable vape,ovns vape kit, ovns juul compatible refillable pod, and so on.
We are also China's leading manufacturer and supplier of Disposable Vapes puff bars, disposable vape kit, e-cigarette
vape pens, and e-cigarette kit, and we specialize in disposable vapes, e-cigarette vape pens, e-cigarette kits, etc.
ovns mesh vape bar,ovns mesh vape pen,ovns mesh vape starter kit,ovns mesh vape disposable,ovns mesh vape box
Ningbo Autrends International Trade Co.,Ltd. , https://www.supervapebar.com