Contact VC Second: MFC Class Foundation, C + + program writing specification Introduction

Source: Internet
Author: User
Tags constructor flush

Because this article is facing the C language Foundation (because I was learned from C), and MFC is the use of C + + class technology built. Therefore, it is necessary here to understand only C friends, the concept of C + + language classes. Friends who are familiar with C + + can skip this section. In general, C + + is backward-compatible C, you can be very effortless to use the C-edited program to get the C + + environment compiled execution. Its C + + is simply adding object-oriented technology (OOP) to C, the concept of class, and it is worth mentioning that C and C + + were invented by Bell Labs in the United States (which I had only known to have invented the telephone).

One, what is a class?

"is a complex type of data that encapsulates different types of data and the operations associated with those data," according to some book definitions. Therefore, the data in the class is hidden, and the class is also encapsulated. "Well, the class, like the one above, has a strong abstraction," he said. Let me use an example to explain the class. Well, we have a species called birds in the world, in the C + + world We can also make a class called birds. It should have a head, torso, legs, guts, and a very important wing. As a result, its classes are described as follows:

class Aves
{
  char m_strHead[10];
  char m_strTrunk[10];
  char m_strCrura[10];
  char m_strWing[10];
  char m_strBowels[10];
};

Ha, such a bird has been established, how to be different from the structure of C. (in C + +, struct and class are basically synonyms, and later on they are different.) If you want to build a bird, do not like C in the trouble to play struct Aves xxx, but directly using aves XXX can be, do not hit the front of the struct or class. Before the concept of bird formation, the bird's wings, body and so on really exist (no one has a doubt?) , but in the people do not know the birds of the feathered flush flush can fly things called what name, also do not know what the meaning of the word wings. Now our C + + birds are in this state, and they are not assigned any values in those member variables. In real life, the specific name of a species is named at the beginning of a class object, which is a process of noun initialization. In the C + + class, when a class object is created, there is always a process of initializing the member variables, so the constructor is introduced. It is invoked when an instance is declared and built (these two have some differences). Our C + + birds can be used to assign names to various member variables to implement:

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_strCrura[10];
  char m_strWing[10];
};

Look how I built a constructor inside the class. The constructor of a class has the same name as its class name and cannot have a return value. We name each member function in the constructor. When Aves Bird (declares a bird object), the constructor of the Aves class will be invoked to assign the Bird.m_strhead,bird.m_strtrunk and other member variables to "head" and "Trunk" respectively. In this way, I hope you have a certain understanding of the constructor. Now that we have constructors that can initialize class members, what do we use to pin the class members? In the end, there is a function called when the class object is created, and what function is the function that is called when the class object is deleted? That is the destructor, whose naming rule is the same as the constructor, except that a ~ (wave number) must be tightened in front of its function name and cannot have arguments. As our class is ~aves (); As for the concrete function of the destructor ... For example, when a piece of memory is applied to a constructor, we have to release the memory in the destructor, otherwise the memory leaks. So when will the declared class object be deleted? To solve this problem, I also need to borrow the concept of a Shena domain (name space). The deletion of a class object is raised when the system executes the pointer leaving the name domain of the declared class object (the valid type of the type can also be interpreted). The concept of domain name is the most real understanding of a pair of curly braces, in which the space within the parentheses is a name domain. (Of course the domain name is not so simple.) If the class itself is a name domain, you can also set a name field for the type declaration setting, which can conflict with the existing type. The real purpose of the name domain is this. Refer to the "C + + standard library", the book building has a translation of Mr. Houtie, for example:

{//名域1
  char * strValue;
  {//名域2
     Aves bird;
     {//名域3
       strValue=bird.m_strWing;
     }
}//<<就在后大括号这里引发了bird对象的析构函数
strValue=”Blue Atlantis”;
}

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.