The following is a review of my personal pair of C . The Lord is commences to the phase object characteristics of C . And the extension of C. As an intermediate language C is very successful we can directly read and write memory through C. For example: INT i; int * pi = & i; the operation of the pointer can almost make C becomes a structured assembly language. C is expanded as C or the efficiency of non-damage C (accurately 5% loss) to achieve the basic characteristics of a face-phase object language should be held. Then there must be all syntax elements of C to join the surface-comparable characteristics.
The following is a brief review, the history of computer language. Early computer programming language is mainly used to control the characteristics of a computer life, and its language is also interacting with the machine. The appearance of assembly language can be used directly to operate the machine code to operate the read and write. With the development of this computer, the computer is not only engaged in scientific calculations. To process more data, and transactions. The emergence of a structured programming language has gradually replaced compilation and machine language. The humanity, and the simulation reality is really a process. Basic structured reasons, order, branches, cycles, basic three structures are processed and combined. Most problems can be handled. The nature of its analysis problem has also become a characteristic of analytical processing realistic transactions from single and machine. As shown below:
It can be seen from the figure that the development of its language is determined by the complexity of more processing problems. That is to say, with the ability to deepen their computer handling problems, the ability to deal with the transactions in real world will inevitably improve, so computer programming languages will develop. The development of computing programming languages is the opposite of its ability to realize the ability of real estats. Therefore, learning C is a method for learning as an object-oriented analysis problem.
Object-oriented C
The following mainly combined with object-oriented basic three (encapsulation, inheritance, polymorphism) basic characteristics for C languages. OK, I will always make a statement on our questions before review, or say, hey, listening to us why I use this thing. We may say directly for structured programming languages. Data and separate operation of data. Everything is composed of three structural streams, branches and cycles. The traditional structural block diagram is very intuitive. When I have something to do, as long as the prior processing procedure, our program will be executed. There is no doubt that this is intuitive and reasonable. But the problem will appear. Listening, maybe you will say that my procedure cannot describe an objective problem other than the algorithm. E.g. If you want to describe a person's behavior, you will find special difficulties, maybe you will write this:
Struct Person
{
Bool sleeporgostate; // Determine the behavior
}
Void Move (Person * P)
{
IF (P-> State)
{
Printf ("OK I'll GO!");
}
Else
{
Printf ("Oh No I'll Sleep");
}
}
. // The call of the main function is as follows
void main ()
{
Person * p = (struct person *) malloc (sizeof (person));
P-> Sleeporgostate = true;
Move (P);
P-> Sleeporgostate = FALSE;
Move (P);
IF (p! = Null)
Free (p);
}
Then you may say that all the problems are all solved. is it? Then there are many objects that can be moved. So maybe you will say you person_move car_move plane_move, etc. . . But think about it, you will find programs and strategies that may have more solving problems. If the data of the object and the operation of the object, it will look very intuitive. So this practice is called package. So the program can become like this:
Class Person
{
PUBLIC:
Bool Sleeporgostate;
PUBLIC:
Void Move (Person * P)
{
IF (P-> State)
{
Printf ("OK I'll GO!");
}
Else
{
Printf ("Oh No I'll Sleep");
}
}
}
// So your main function can be written like this
void main ()
{
Person * p = new person;
P-> Sleeporgostate = true;
P-> Move ();
P-> Sleeporgostate = FALSE;
P-> Move ();
IF (p! = null)
Delete P;
}
The above two sets of code you can find out, just put the properties and methods in the class. For the outside of the class, it can be seen as a whole. Attributes and operations for attributes are responsible by object life, and you will also find another problem. Where is Malloc? How can I assign a storage space problem. How do I release the space occupied? C will provide us with all environments to do these things. The new operator NEW is used to assign a storage space. Delete's appearance also replaces the free () function call. Everything is very intuitive. The object was created by the Class Match and destroyed as the use ended. Very natural rules.
Similarly, as you deepen, you will find some problems will continue. Many people will say that this world is complicated because they are always changing. New things will also emerge. When you find that the results you have analyzed different from the perspective of your point is completely different. For example, you can use nationality, or gender to distinguish between different people. Then you will find you to override your source code. To add nationality or gender in the Person class to expand. OK If a little class may do this, you don't have a problem. But it is a large scale problem. You will find that you will be killed by your questions rather than the problem. So you are frustrating. I hate such a situation. Ho my god may you give up trying new ideas. However, c creators will not be so careless. He is clearly an object-oriented theory. The appearance of inherits is natural. OK. The problem has been resolved. You will find that this is the case. So your code is more intuitive
Class WorldPerson: Public Person
{
PUBLIC:
CHAR * m_COUNTRY;
BOOL M_SEX;
PUBLIC:
// Other Operation
}
// ok Your main function can be written like this
Int main (int Argc, char * argv [])
{
WorldPerson WP;
WP.M_COUNTRY = "China";
Cout << wp.m_country << Endl;
WorldPerson * PWP = New WorldPerson;
PWP-> m_country = "china";
Cout << pwp-> m_country << Endl;
IF (PWP! = NULL)
DELETE PWP;
Return 0;
}
OK, everything is very reasonable, but if there is a careful programmer written like this
WorldPerson WP;
Cout << wp.m_country << Endl;
Day. He will call it a little. what's happening? listen. Common sense tells us. A pointer either points to a null or must point to a memory space that has been assigned to it. Otherwise, everything is unable to determine, it is best to determine if the pointer is empty before you use the pointer.
WorldPerson WP;
IF (wp. M_country! = Null)
Cout << wp.m_country << Endl;
Else
WP. m_country = new char [20];
If you are null, you assign an address space for it. This is OK. Nothing special question is not? But such a careful question may be committed. And you must consider another question. That is. Who is responsible for the member variable? If you don't do your problem, you will not appear soon, the more you run, the more resources you usually have. So C provides us with two other ways. Tell your member data by the class responsible for initialization. The same is also responsible for cleaning up. So your class can be written like this: Class WorldPerson: Public Person: PUBLIC PERSON
{
PUBLIC:
CHAR * m_COUNTRY;
BOOL M_SEX;
PUBLIC:
WorldPerson ()
{
This-> m_country = new char [20];
STRCPY (this-> m_country, "China");
THIS-> M_SEX = TRUE;
}
~ WorldPerson ()
{
DELETE [] THIS-> M_COUNTRY;
}
PUBLIC:
// Other Operation
}
Does this should have no problem? Create and initialize. It is tightly tone to the resource occupied and cleaned up. Isn't there too many questions? Yep. . . Maybe you have considered a problem. That is inheriting his life. Who is responsible for the initialization of the parent class? Hey, your head may be big. Do you want to consider this problem? I know not, I know. Don't complain. If you want to use the face-to-face features, you must pay maintenance costs. You must call the constructor of the parent class. So your constructor write this:
WorldPerson (): Person ()
{
...... ..
}
The constructor of the parent class will be called first. no doubt. All this is like natural law. The father was created first. It is then derived from it. Is it natural? correct. So is the order of death? Oh No you are wrong. Not as you think. The fact is often the opposite you want. Cleaning function Call starts from subclats. Then the parent class. The reason is very simple to think about it. No matter whether the reference is or a pointer. The base address that points to the parent is all parent, and the subclass is a supercoming of the parent class, which is greater than or equal to the parent class. Therefore, the destruction of the pointer or reference should then first destroy the scope of the subclass and then the parent class. The OK test is as follows:
Int main (int Argc, char * argv [])
{
WorldPerson WP;
Return 0;
}
The output is as follows:
Person
Worldperson
~ WorldPerson
~ Person
OK issues have been deepened. You will find that your current method can't solve the problem life. For example, you will find a lot of questions at some time. You can decide its essence early in the early days. Just getting late, you hope to have more expansion. For example, a certain company decided that all employees of the company have proposed a statement. obviously. Proposing the report is something that every employee will do. Just proposed report content is very different. The contents of the salesperson and technical personnel and documentation are very different. This must tell yourself, ok I have to join a way to write a report for no object. Yes. You can do this. So your code is as follows:
Class Seller
{
PUBLIC:
Char * m_name;
PUBLIC:
Void Print_Report ()
{
Cout << "Use the printer" << ENDL;
Cout << "" promised sales record "<< Endl;
}
}
Class Programmer
{
PUBLIC:
Char * m_name;
PUBLIC:
Void Print_Report ()
{
Cout << "Use the printer" << ENDL;
Cout << "" "" "" "" 程序 程序 记 "<< endl;
}
}
Look at your code well. How much repeat? If you record more, you repeat more. Think about it carefully. These two classes only different formats of print recording. right. Then we can clearly analyze a parent class. Employee provides a way. It is only the specific difference between the late resolution. as follows:
Class Employee
{
PUBLIC:
Char * m_name;
PUBLIC:
Void Print_Report ()
{
Cout << "Use the printer" << ENDL;
}
}
Class Seller: Public Employee
{
PUBLIC:
Void Print_Report ()
{
Employee :: Print_Report ();
Cout << "" promised sales record "<< Endl;
}
}
Class Programmer: Public Employee
{
PUBLIC:
Void Print_Report ()
{
Employee :: Print_Report ();
Cout << "" "" "" "" 程序 程序 记 "<< endl;
}
}
OK You directly call the method of the parent class. So there is no problem? Yep. . . . Listen to this. Your call member may be like this:
Int main (int Argc, char * argv [])
{
Employee * e = new programmer ();
E-> Print_report ();
IF (e! = NULL)
Delete E;
Return 0;
}
Output results:
Use printer
Day. . Your result is not as you think. . . You are running their hopes to be able to install a practical Programmer object. The pointer E of the parent class can be called to the correct function. But the result is that you have not succeeded. Oh is right. C is supported. Yes, then you have to use the Virtual keyword. So your parent class void print_report ()
Declimination must write Virtual void print_report ()
Then your run results are correct.
OK, the basic surface of the C language, I have already basically telling it. It is only some related grammar to not say.