C ++ is an object-oriented programming language, which is often used for object-oriented program development. However, can process-oriented languages such as C be used for Object-Oriented Programming? The answer is yes. Although C language does not have many features of object-oriented language, including its core features, namely encapsulation, inheritance, and polymorphism, we all know that the essence of object-oriented programming is actually a way of programming thinking, it is not necessarily related to the language in use, but the use of object-oriented languages can enable programmers to better implement this way of thinking.
"Object" is an instance of "class. The constructor is automatically called when an object is generated, and its destructor is automatically called when the object dies. This is a concept in C ++. It sounds mysterious and mysterious. Why is it so intelligent? This is especially true for beginners. In addition, a new language always produces many concepts, such as encapsulation, inheritance, polymorphism, and overload. There are many concepts, which give us a mysterious and advanced feeling. In fact, it is indeed a high level, but it is not discussed here. Here we want to explore its nature, even if it is "evident", we can also "look at it and see it all ".
The implementation of C ++ constructor and destructor is at the compiler layer. The compiler generates code to call the constructor at the object definition, and generates the Destructor code at the exit scope. So it looks "automatic.
For example, there is a game class:
Class game
{
PRIVATE:
Game ();
~ Game ();
Int m_var1;
Int m_var2;
Public:
Void play ();
}
Its implementation is:
Game: Game ()
{
M_var1 = 1;
M_var2 = 2;
}
Game ::~ Game ()
{
M_var1 = 0;
M_var2 = 0;
}
Void game: Play ()
{
M_var1 ++;
M_var2 ++;
}
Void testgame ()
{
Game g;
G. Play ();
}
If C is used for simulation:
# Include <stdio. h>
# DEFINE _ debug
Typedef struct _ game;
Struct _ game
{
Void (* gameconstructfunc) (game * This);/* constructor pointer */
Void (* gamedestroyfunc) (game * This);/* destructor pointer */
Int m_var1;
Int m_var2;
Void (* playfunc) (game * This);/* member function play pointer */
};
/* Common behavior of abstract objects: This example only contains constructor and constructor */
Typedef struct _ object
{
Void (* virtualconstuctfunc) (struct _ OBJECT * This );
Void (* virtualdestroyfunc) (struct _ OBJECT * This );
} Object;
/* Constructor implementation */
Void gameconstruct (game * This)
{
This-> m_var1 = 1;
This-> m_var2 = 2;
# Ifdef _ debug
Printf ("construct/N ");
# Endif
}
/* Destructor implementation */
Void gamedestroy (game * This)
{
This-> m_var1 = 0;
This-> m_var2 = 0;
# Ifdef _ debug
Printf ("Destroy/N ");
# Endif
}
/* Implementation of the member function play */
Void play (game * This)
{
This-> m_var1 ++;
This-> m_var2 ++;
# Ifdef _ debug
Printf ("play/N ");
# Endif
}
/* Construct an object. The constructor can only be called manually because it does not have the help of the compiler,
In actual applications, an "Object Manager" exists on all objects ",
It has the behavior of constructing objects */
Void Createobject (Object * OBJ)
{
OBJ-> virtualconstuctfunc (OBJ );
}
/* Destructor. The constructor can only be called manually without the help of the compiler,
In actual applications, an "Object Manager" exists on all objects ",
It has the behavior of the Destructor */
Void destroyobject (Object * OBJ)
{
OBJ-> virtualdestroyfunc (OBJ );
}
/* Use instance */
Void testgame ()
{
/* Define object */
Game G = {
Gameconstruct,
Gamedestroy,
0,
0,
Play
};
/* Construct an object */
Createobject (Object *) & G );
/* Call object behavior */
G. playfunc (& G );
/* Destructor */
Destroyobject (Object *) & G );
}
/* Test Program */
Int main (void)
{
Testgame ();
Return 0;
}
In the above Code, are there some familiar things? For example, this pointer.
The C ++ language relies on compilers to implement similar functions. Of course, it is more powerful. Most of the powerful functions of the language are implemented at the compiler layer, because once the source program is converted to a machine code, the features of the language have been lost (except for languages with interpreters, such as Java, it has virtual machines, which correspond to the running stage of the program ). For example, in C ++, the restrictions on data access permissions (private and public) and the implementation of inheritance are all implemented by the compiler. Languages and compilers complement each other and are indispensable.
At present, C language programming on embedded devices also plays an important role. Using object-oriented ideas to develop C Programs is of great benefit to some complex applications. Some embedded game development applies this idea in the design of game engines. In addition, similar applications can often be seen in programming of operating systems and system software. If you are interested, you can look at their source code.