Contact VC, 2: 1

zhaozj2021-02-16  56

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;

}

void main ()

{

Aves bird;

Bird.FLY ();

Bird.Run ();

}

We can see the operation of the entire class under the run of the main function. At the time of the statement, a bird is born, the constructor is run, output "a bird born". When the object is deleted, the destructor is executed and outputs "a bird Die". Please note that in the class declare, I added a public: and private: such a statement. This is a statement that sets access to access, which is the permission that will be set from all of its servants until the next access permissions statement, and the member function is set to its specified permissions. If public is set to public permissions, the member of this permission can be used outside, and the private sets private permissions, and the member set to such permissions cannot be used outside, only can be used within its member function. For example, use Bird. M_strbowels is illegal because it is a private member variable. Because the visceral of the bird is invisible outside the outside, it can only be used by the bird itself.

As mentioned earlier, Struct and Class in C are basically synonymous. In the Class If the right keyword is not specified, the default permissions are private, and the default permissions in Struct are public.

When the member function needs to use other members, you can write its name directly, such as using its member variables m_strcripra in the run () function. In fact, a complete way should be this-> xxx member. This is a pointer in this class, in this class is Aves *. It represents the pointer of the current instance object, and when using Run () in the BIRD object, this is & bird. Can be rewritten as Void Aves :: run ()

{

Cout << "i can run by" << this-> m_strcriprura << Endl;

}

Telling so much, I believe you should write a class? Use your VC to create a Win32 Console Application Engineering, add a C Source File file to the project with a new files to try your own C program.

Then, the inheritance of the class is coming.

Take a bird to give examples. Birds can also be finely divided into many categories, chickens, ostrich, what. They are all birds, which have common features and behaviors as a whole, but there are other differences. There will be similar places in the C class. How to do? Re-write a class, re-write those who are the same members? At this time, you need to inherit. We can write such a class:

Class Ostrich: Public Aves

{

}

The inheritance is:

Class derived class name: Permission keyword (generally public) base class name <, base class name 2 <..., base class name n >>

This Ostrich class is inherited in the Aves class, and the Ostrich class now has all members in the AVES class. If you declare an object Aostrich from the Ostrich class, you can call it directly Aostrich.Run (). In fact, in calling the RUN member function in the AVES class. If you add a member to the Ostrich class, you will add members on the basis of the AVES class.

It is necessary to say that there is a private member M_STRBOWELS in the AVES class. Because it is a private member, it is also invisible for its partial ostrich. To solve this problem, you need to change the private key in the AVES class to the protected key. Describe M_StrBowels member as a protection. Protective type, it is visible to it, which is invisible to external and private.

In reality, the ostrich will not fly, the sound is different, then we need to change its behavior. code show as below:

Class Ostrich: Public Aves

{

PUBLIC:

Void Tweet ();

Void fly ();

}

Void ostrich :: tweet ()

{

COUT << "gungugugugugu" << endl;

}

void ostrich :: fly ()

{

COUT << "i can't fly by" << m_strwing << endl;

}

Because it is too long, it is segmented.

转载请注明原文地址:https://www.9cbs.com/read-24715.html

New Post(0)