Contact VC, 2: 2

zhaozj2021-02-16  51

Connect the above section.

We wrote a fly () and tweet () member function on the basis of the Ostrich class, which is the same as the base function name of the base class. Then they will overwrite the base class, if the FLY () of the Ostrich class is called again, the two functions we write will call us.

The basic code is as follows:

#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];

protected:

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;

}

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;

}

void main ()

{

{

Aves bird;

Bird.FLY ();

Bird.Run ();

Bird.tweet ();

}

Cout << ====================== "<< ENDL;

{

Ostrich aostrich;

Aostrich.Fly ();

Aostrich.Run ();

Aostrich.tweet ();

}

}

In the main function, I added two pairs of braces, please analyze the Bird, Aostrich living area.

The above is an example of a single inheritance, as for the inheritance explanation theory. Everyone can try themselves. Many inheritances will occur in COM writing in the seventh part.

Inheriting derived remember, derived objects are also objects of their base classes, and the pointers of the base class can point to objects that are derived. If we are an Aves * lpbird; pointer, then we write lpbird = & aostrich is legal because ostrich is also a bird. Now, to mention the final important concept of the class is the virtual member function. In the last paragraph, a base class pointer can point to a derived class object. If LPBIRD points to Aostrich, then call lpbird-> fly (); what is the result? Wow, it is "I can fly by wing", come and see, the bird we refer to actually fly! Obviously this is what we don't want to see. In order to solve this problem, I define all member functions as the Virtual virtual function in the AVES class declaration.

Class Aves

{

PUBLIC:

Aves ();

~ Aves ();

Virtual void tweet ();

Virtual void run ();

Virtual void fly ();

Char m_strhead [10];

Char M_STRTRUNK [10];

Char m_strcripra [10];

Char m_strwing [10];

protected:

Char m_strbowels [10];

}

Try again, the result is the "I can't fly by wing" we want. why?

Is such that. When there is virtual function (including base classes), all virtual functions of this class, a function name, and virtual functions of the base class will be established for all virtual functions of this class. When an object is executed, the system will check this virtual function table (VTABLE), find the function address corresponding to this function name, call it. When the derived class adds a function with the base class virtual function, the system will automatically set it to virtual functions. And rewrite this function address to the virtual function table. If you call this virtual function, you will call the newly added virtual function. Like the above example, when calling lpbird-> fly (), the system will first check the virtual function table of the object to point to the object, without not at the 3721, directly call the function of its present class. The sample code is as follows:

#include

#include

Class Aves

{

PUBLIC:

Aves ();

~ Aves ();

Virtual void tweet ();

Virtual void run ();

Virtual void fly ();

Char m_strhead [10];

Char M_STRTRUNK [10];

Char m_strcripra [10];

Char m_strwing [10];

protected:

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;

}

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;

}

void main ()

{

{

Aves bird;

Bird.FLY ();

Bird.Run ();

Bird.tweet ();

}

Cout << ====================== "<< ENDL;

{

Ostrich aostrich;

Aostrich.Fly ();

Aostrich.Run ();

Aostrich.tweet ();

Aves * lpbird;

LPBIRD = & aostrich;

LPBIRD-> fly ();

}

}

For more specific cases, please refer to the "resolution dynamic linkage" of VCKBase 12th. For other C grammar, please check C textbooks yourself.

So what do MFC? Simply, the MFC class is only packaged together with many associated API functions. WinAPI in WinsDK is some zero-dispersed functions, and most of them have a parameter is the handle of its service object. For example, the CREATEWINDOW function will need a handle to return to a window handle to express its created window object, the showwindow function requires a window handle to specify which window to change the display status, the closewindow function requires a window handle to specify which window want is closed. It can be understood that the handle is rotating around the function, and the handle is centered on a function. The MFC is a member function packaged together into a member function together, and each class will have a protected handle member variable to save the service object represented by the current class, you can use it on the external call. Class objects look into their service objects, which can be seen as their service objects. More image and understanding than using WinAPI. Let's make an example of a bit.

SDK Writing

HWND HCURRENTWND;

HcurrentWnd = :: CreateWindow (...);

:: ShowWindow (hcurrent, sw_show);

:: CloseWindow (Hcurrent);

MFC Writing

CWND CurrentWnd;

CurrentWnd.createWindow (...);

CurrentWnd.showWindow; SW_SHOW;

CurrentWnd.closeWindow ();

How, the concept of mfc without boring and fragmentation. We can imagine a class object is an service object, which itself has many ways to control it. This is the main purpose of making MFC.

All MFC class base classes are COBJECT. You can use the COBJECT's pointer to all MFC classes. The CWND class is all the API functions about the window. All controls are derived from this class such as CEDIT, CButton, CDIALOG, CFRAMEWND, CMDIFRAMEWND, CMDICHILDWD, CVIEW, CDIALOG and other MFC, such as message mapping, cantimeclass, etc. in "deep shallow MFC", "C technology insider" There is a detailed discussion and explanation, strong recommendation. I just see the door of these books. Various features of the MFC class also see MSDN.

The next section will explain an MFC dialog program in detail.

Below, I want to introduce some C programs to prepare for the specification.

First, change, often, and parade recommended:

1. Often, the variable should define the most in front of the most in front of the function body or a pair of braces, the whole, the variable is the front of the entire document. This makes it easy for management and maintenance.

2. The declaration, the parameters should be used to use Hungarian nomenclature. To change, the parameters add an appropriate prefix, and naming with meaningful spelling nomenpanic English words, each English morphology should be capitalized. Such as: m_ncount; indicating this is a member of a class, for integer, is used by doing counters.

Common prefixed:

Prefix

Indicate content

Prefix

Indicate content

_ Or AFX

Expressed as global

M_

Represents a member of a class

b

Boolean

hide

Indicated as a handle

C or CH

Be expressed as a character type

l

Be expressed as a long

CLR

Represents 32-bit color value

n

Indicate

CX or CY

Indicates the horizontal or vertical value of the coordinate

P or LP

Pointer

w

Represents a word (Word) type

SZ

Represents a string ending with 0

Str

Representation as a cstring character type

DW

Represents a double word (DWORD) type

3. Constants should be defined in constings instead of using pre-processing instructions #define. And the constant name should be capitalized.

4. If it is a global, the variable should be prefixed.

Second, the suggestion of the function:

1. The definition of the parameters should be attached to the humanized, and the output parameters are before, the input parameters are behind.

2. If the parameter is a pointer, it is for input, it should be added before the type of time to prevent this pointer from being accidentally modified in the function.

3. Function Name If it is a global prefix AFX, the function name is named with meaningful spellful verb English words or phrases, and each English morphine should be capitalized. Such as: AFXGetMessage (), Close ().

4. The return value of the general function is best used to return the error flag, and the true return value should be returned by the output parameters.

5. In the first few lines of the function, the entry parameters should be valid for validity.

6. The size of the function body is small, and as much as possible within 50 lines of code. Otherwise, splitting should be split.

7. Do not declare the use of static variables in the function body, which will be difficult to control.

Third, the advice:

1. All member variables should have a M_ prefix.

2. The statement of public, protected, final private, pre-priority, and post members should be followed in the statement of the statement.

3. It is not possible to operate in a statement of the class, which is incorrect and the compiler is not allowed. That is, initialization (eg int m_ncount = 0;) is not.

4. The statement should be written in a statement that is written in a file named name, suffix .h, should be implemented in a member function written in a file with a class name and .cpp. And write the #include "class name .h" on the first line of the .CPP file.

Fourth, other recommendations:

1. In FOR, IF, Do, WHILE and other statements, whether or not the following statement is a line, you must enclose it with braces. 2. It is not possible to write multiple statements on a line, which will reduce the readability of the program.

3. The annotation should be added in a block or line that is not easy to understand (such as an inline collection statement). Don't add comments on the statement that is easy to understand, (such as: i ;)

4. In the programming, you should think of the future reproducibility and leave the rest of the future.

5. In the request of technical procedures, try to complete the functionality as close as possible. Do not pursue visual interface effects unless necessary. Because the interface code is much more confusing than the function code. Will make the code are not easy to maintain.

6. Some common functions should be organized into classes that can be used directly, not only eliminating effort and makes the code look simple. Such as interface code, etc.

7. Don't write code directly when you are officially programmed. Program should be planned to be implemented, and its algorithms should be planned to implement, and then go to hand. Because it is important to go to code in programming, it is a method of implementation. And directly write code, will definitely lead to modification modification of the program code, so that the code looks very messy.

8. It is accustomed to writing a document at most time because the key is that it will be able to be accepted.

9. To use 70% of the time to envision the program's algorithm, use 27% of the time to write a document, only 1% time to write the code, and finally use 2% time to debug code.

The second part is finished.

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

New Post(0)