The second part of the MFC foundation, C programming specification introduction
Since this article is facing the C language basis (because I am learned from C), the MFC is built using C technology. Therefore, it is necessary to understand the friends of C, popularize the concept of C language in the C language. Friends who are familiar with C can jump this part.
In general, C is compatible down, you can get the program that C environments can be compiled with C environments. Its C is only based on C , which is the concept of class, and it is worth mentioning that C and C are Bell Laboratory by the United States (I only know the call before the call. )invented.
What is a class? According to some books, "is a complex data type, which is a collection of different types of data and operations related to these data. Therefore, the data in the class has hidden, class also With encapsulation. "Well, the class is like the above sentence, it has strong abstraction. Let me use an example to explain the class.
Well, there is a biological species in the world called birds, and we can also make a class called birds in C . It should have a head, there is a torso, legs, with internal organs, and a very important wings. Thus, the class is as follows:
Class Aves
{
Char m_strhead [10];
Char M_STRTRUNK [10];
Char m_strcripra [10];
Char m_strwing [10];
Char m_strbowels [10];
}
Ha, such a bird is established, how is it in combination with C in C? (Struct and Class in C are basically synonymous. After a while, I will say what they have different.) If you want to build a bird, don't use Struct Aves XXX like C, but use Aves directly. XXX is OK, do not play the front Struct or Class.
Before humans form the concept of birds, the bird's wings, the body, etc. are really existing (no one has a doubt?), But in people don't know the bird's fans, you can fly. What is the name of the thing, you won't know what the word wings is mean. Now our C birds are in this state, and there is no value in those members variables. In real life, a specific name in a species is initially named in a class object, which is an initialization process. In the C class, there is always a process of initializing the members variables when establishing a class object, so the constructor is introduced. It is called when an instance is declared and established (these two have some differences). The assignment name of each of our C birds can use it to achieve:
Class Aves
{
Aves ()
{
STRCPY (M_Strhead, "HEAD");
STRCPY (M_STRTRUNK, "TRUNK");
STRCPY (M_STRCRURA, "CRURA");
STRCPY (m_strwing, "wing");
STRCPY (M_STRBowels, "Bowels");
}
Char m_strhead [10];
Char M_STRTRUNK [10];
Char m_strcripra [10];
Char m_strwing [10];
}
Hey, how do I build a constructor in the class. The name of a class constructor is the same name with its class name, and does not have a return value, and Void is not. In the constructor, we name them with each member function. When AVES BIRD; (declare a Bird object), the constructor of the AVES class will be called, assigning Bird.m_strhead, bird.m_strtrunk, etc., is assigned to "Head", "Trunk". In this way, I hope that everyone has a certain understanding of the constructor. Since there is a constructor to initialize class members, what is the use of class members? To put it bluntly, there is a function called when it is built, what function is called when the class object is deleted? That is the destructor, its naming rules are the same as constructor, but only need to take one ~ (wavy number) before its function, and cannot have parameters. As our class is ~ Aves ();
As for the specific role of the destructor ... For example, when you apply for a memory in the constructor, we must release this memory in the destructor, otherwise the memory will be leaked.
So when will I trigger a class object deleted? To solve this problem, I also need to borrow a concept of name space (Name Space). When the system executes the name of the class object where the pointer leaves the declaration, the deletion of the class object is triggered (valid type of type can also be explained so).
The most real name of the concept of the name domain is a pair of braces, and the space in parentheses is a domain. (Of course, the name is not as simple. If the class itself is a name domain, you can set a name domain yourself, used for type declaration settings, you can conflict with existing types. The name of the real use is this. Specific use Meaning See "C Standard Library", Book Building has a translation of Mr. Houjie) such as:
{/ Name 1
Char * Strvalue;
{// Nummer 2
Aves bird;
{// Name 3
Strvalue = bird.m_strwing;
}
} // << Just introduce the destructive function of BIRD object here
Strvalue = "blue atlantis";
}
There are three famous fields here, I declare a bird in the famous domain 2. Because the name domain 3 is also included in the name domain 2, the space in the name domain 3 is also a name domain 2, so that the bird object is referenced in the domain 3 is correct. When the execution pointer leaves the name of the domain 2, the C system will remove all the variables and objects declared in the name domain 2.
When the object is deleted, the destructor is first executed, and then the system will then release the memory space occupied by the object.
So when executed to Strvalue = "Blue Atlantis"; this little bird does not exist, then refer to it will compile errors.
In addition, it is necessary to talk about the establishment of an object. One is the same as a variable, which is the same as the AVES BIRD, and the other is to establish an object with a new statement. Such as:
Aves * bird;
Bird = new aves ();
The new statement follows a class constructor that will build an object in memory and return this object's pointer. The object that is built does not have a limitations of the domain space. If you want to delete this object, you must manually use the DELETE statement. Such as:
DELETE BIRD;
DELETE follows with pointer variables to the object to be deleted. Note that the type of this pointer variable directly affects the destructor used when the object is deleted. So, what type of object, what type of pointer is used to point to deletion.
Real birds should be flying (most), you can make a call, you can run on the land (at least you can jump). So we should also let our birds can also jump, you can fly. So I want to add member functions to the class. The declaration member function can have two methods, one is inside the category of the statement, a declaration method of constructing a function in the above example, and the other is outside the class's declaration body. The external performance write method is the return value class name :: function name (parameter list). Note: It is due to two colons. The following code is to use the second declaration method (of course, the two methods can be mixed):
#include
#include
Class Aves
{
PUBLIC:
Aves ();
~ Aves ();
Void Tweet ();
Void Run ();
Void fly ();
Char m_strhead [10];
Char M_STRTRUNK [10];
Char m_strcripra [10];
Char m_strwing [10];
Private:
Char m_strbowels [10];
}
Aves :: Aves ()
{
STRCPY (M_Strhead, "HEAD");
STRCPY (M_STRTRUNK, "TRUNK");
STRCPY (M_STRCRURA, "CRURA");
STRCPY (m_strwing, "wing");
STRCPY (M_STRBowels, "Bowels");
Cout << "A Bird Born!" << Endl;
}
Aves :: ~ Aves ()
{
Cout << "A Bird Die!" << Endl;
}
Void Aves :: tweet ()
{
COUT << "jijijijijiji" << Endl;
}
Void Aves :: run ()
{
COUT << "i can run by" << m_strcripra << Endl;
}
Void Aves :: fly ()
{
COUT << "i can fly by" << m_strwing << Endl;
}