Visual C ++MFC Guide (2): C ++ Points

zhaozj2021-02-11  149

Visual C / MFC

guide

Second lesson:

C

Point

If you want to use Microsoft Visual C , you will help with the content of the class in C will have great help. If you are used to using simple C, you only have practical to master the processing of class. Before starting VC , let's review you should figure out the content of the class.

To a large extent, it is a structure. We will start from an example rather than just explain the rules. Write a class to describe the straight line. Definition in .h file:

Class Cline

{

INT m_nx1;

INT M_NY1;

INT M_NX2;

INT M_NY2;

PUBLIC:

// conntructors

Cline ();

CLINE (int X1, int y1, int x2, int y2);

// deStructor

~ CLINE ();

// set the line data

Void setPoints (int X1, int y1, int x2, int y2);

// Draw the line

Void Draw ();

}

Short talk about naming practices. The name of the class is usually headed by 'c'; the member variable uses the prefix 'm_', followed by using a letter according to Microsoft's habit to specify the data type, then the name of the variable. All words begin with uppercase. I recommend this Microsoft standard (called Hungarian law) because it is wide and easy to understand. In this case, you will see M_ppoint, you will think of this is a member variable and pointing to the point type pointer; seeing fdata will think of this is a floating point value.

Back to discussions about classes. The endpoint of the integer variable recording line. Note that they are placed before 'public:', indicating that programmers using this class cannot use these variables directly, they are not "disclosed". Those functions under the public statement are publicly used. The two of the two are called constructors, which are always executed when a new CLINE object is created. Below is some time they call:

// this calls cline ()

Cline myline;

// this is a pointer to a cline class

Cline * PMYLINE;

// this calls cline ()

PMYLINE = New Cline;

// this is a pointer to a cline class

Cline * PMYLINE;

// this Calls Cline (int X1, int y1, int x2, int y2)

PMYLINE = New Cline (0, 0, 10, 10);

// this Calls Cline (int X1, int y1, int x2, int y2)

Cline Myline (0, 0, 10, 10);

All of these have established a straight line. Some straight lines are initialized to the default settings for new parameters. Keyword "new" creates a new object in C , similar to Malloc in C. You must use "DELETE" to all objects using "new", just like Free in C. Not only the class is like this, the other data type is the same. I assigned an array with 100 integer data:

// a pointer to some integers

INT * PNUMBERS;

// Make Memory for 100 of them

PNumBers = new int [100];

// set the first element to 0

PNUMBERS [0] = 0;

// set the last element to 99

PNUMBERS [99] = 99;

// free the memory.

DELETE [] PNumBers; Note in DELETE [], this is to delete the entire array on the program. If you write "Delete PnumBers;", only the first element is removed. This will cause memory leakage.

Sorry, let's go back to the constructor of CLINE. A straight line is automatically used for constructor when it is created, and the code is like this:

Cline :: cline ()

{

m_nx1 = 0;

m_nx2 = 0;

m_ny1 = 0;

m_ny2 = 0;

}

Cline :: Cline (int x1, int y1, int x2, int y2)

{

m_nx1 = x1;

m_nx2 = x2;

M_NY1 = Y1;

M_NY2 = Y2;

}

We see that in addition to putting the class name and two colons (cline: :) ​​in front of the function name. The function is very similar to the standard C function. A difference is that the constructor has no return value, and the destructor is also the same. The destructor is automatically called automatically after our CLINE object is deleted or out of the living space. such as:

// this is a pointer to a cline class

Cline * PMYLINE;

// this calls cline ()

PMYLINE = New Cline;

//Memory for the class is cleared up and ~ cline () is Called

DELETE PMYLINE;

{

// this calls cline ()

Cline myline;

}

// this '}' Ends the section of the program where myline is

// Valid. ~ Cline () Will Be Called. (MYLINE GoES out of 'scope ")

For us this class, ~ cline () does not have to do anything. But you can put the cleaning code here, like the memory allocated in the recycling class. I don't have to clean up now, the function is empty:

Cline :: ~ cline ()

{

// do nothing

}

Now let's fill in another two functions:

Void Cline :: setPoints (int X1, int y1, int x2, int y2)

{

m_nx1 = x1;

m_nx2 = x2;

M_NY1 = Y1;

M_NY2 = Y2;

Return;

}

Void Cline :: DRAW ()

{

// psuedo code here, these Are Operating System, THESE ARE OPERATING SYSTEM

// Functions to draw a line

MoveTo (m_nx1, m_ny1);

LINETO (M_NX2, M_NY2);

Return;

}

How do you call these? There are two examples here. Another pointer using a pointer:

Cline * PLINE = New Cline (0, 0, 10, 10);

PLINE-> DRAW ();

DELETE PLINE;

Cline myline;

MYLINE.SETPOINTS (0, 0, 10, 10);

MYLINE.DRAW ();

This class is completed. This class can now be in other classes. You can build a square csquare with 4 straight lines:

Class csquare

{

Cline M_Linetop;

Cline M_LINELEFT;

Cline M_Linebottom;

Cline M_Lineright;

// ...

}

There is also better, according to the characteristics of class, you can use the CLINE class to build your own class. This usage is too much in VC. For example, if you want to draw a straight line in the program, you will want to use a straight line, but this is still an important feature that cannot set the color of the straight line. Of course, you don't have to write new classes, the simpler way is to inherit the CLINE class. Like this: Class Ccolorline: Public Cline

{

PUBLIC:

Void Draw (Long Color);

}

How is it now? This class has all functions of the Cline class, and we can use another DRAW () function that can set the color, the code in the CPP file is like this:

Void ccolorline :: Draw (long color)

{

// psuedo code here, these Are Operating System, THESE ARE OPERATING SYSTEM

// Functions to draw a line

SetColor (Color);

Cline :: DRAW ();

Return;

}

Now we have another class of all functions and add an additional DRAW function. But this function is the same name with the original Draw function! It doesn't matter, C is smart enough, it can silence: If you call Draw (Color) Use the new function and if you call Draw () use the old function. In the code, Cline :: DRAW () may make you feel unfamiliar. This is a line function that tells the program to call the base class. We don't have to write Lineto and Moveto's code at the time of time, very good, isn't it? Now we can do this:

Ccolorline myline;

MYLINE.SETPOINTS (0, 0, 10, 10);

// Assuming 0 Is Black, this will draw a black line.

MYLINE.DRAW (0);

Of course, I have omitted many aspects here. For example, define an operator, function overload, virtual function, protection, and private member ... and more. But it is enough to let you continue.

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

New Post(0)