VC ++MFC Tutorial 2 (English)

zhaozj2021-02-08  225

Lesson 2: C Essentials

If you want to use Microsoft Visual C , it helps a ton if you really know C . Everything is about classes. If you are used to plain C, you will not really see the big deal with classes until you use them for a while Let's review what you need to know about classes to get Started with VC .

A class is a structure for the MOST Part. Let's work with an example instead of me just telling you rules. Let's make a class to represent a line. In the .h file you wouldding define the class as Follows:

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 ();

}

A quick word about naming conventions. Class names usually start with 'C' and the member variables usually are prefixed by a 'm_'. Then in the microsoft way you will have a letter to let you know what data type the name is and then the name of the variable. Capitalize the letter of all new words in the name. Do not use underscores and stuff like that. I recommend this Microsoft standard (called Hungarian notation) since it is widely accepted and very easy to read. If you See m_ppoint, you would be a point. if you see fdata, you would atum value.

Back to our class The int variables are the end points of the line Note that they are before the 'public:'... Part This means that a programmer using this class will not be allowed to manipulate these guys directly They are not for. 'public' use The functions under the public statement are for public use The first three are called constructors These functions are called anytime a new CLine class is created Here are some examples when the are called:.... // 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 construct a line. Some initialize it to its default settings and others copy coordinates. The 'new' keyword is used to create new things in C , like malloc in C. You need to call 'delete' for everything you say new To, Like Free In C. This Goes for classes as well as os. I Could Allocate An Array Of 100 Integers with:

// 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;

Notice the [] after the delete This is to tell the program to delete the entire array If you say 'delete pNumbers;'.. You will only free memory for the first element You will then be 'leaking' memory Memory leaks are.. when you forget to delete memory. This may end up crashing your computer if you use all the computers memory.Sorry, let's get back to the constructors for CLine. The code for these constructor functions which automagically get called when a new line is created will Look Like:

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;

}

Notice that the function declaration is much like a regular 'C' function except that we put the class name and two colons in front of the function name (CLine: :) ​​One difference with constructors is that they do not have a return value. ...............................................................................................................................................

// 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 our class, ~ CLine () does not need to do anything However, sometimes you may want to put your cleanup code here Like deleting any allocated memory in your class Since we have nothing to do out function code is empty...:

Cline :: ~ cline ()

{

// do nothing

}

Let's Fill in The Other 2 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

// Functions to draw a line

MoveTo (m_nx1, m_ny1);

LINETO (M_NX2, M_NY2);

Return;

}

HOW WOULD I CALL THESE FUNCTIONS? Here Are A Couple of Examples. One With Pointers and One forout.

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

PLINE-> DRAW ();

DELETE PLINE;

Cline myline;

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

MYLINE.DRAW ();

That's it for the class. Now this class can be used in other classes. You can IMagine a csquare Class That Has 4 Cline Classes in IT:

Class csquare

{

Cline M_Linetop;

Cline M_LINELEFT;

Cline M_Linebottom;

Cline M_Lineright;

// ...

}

OR Better Yet, The Point of All of this Class Stuff, You Can Use the cline class to make your owned to draw line in Your Program Wanted to Draw Lines in Your Program, And you wishht Class Might Be nice, but it is missing an import the line color. instead of write a whole new class, you can simple inherit the cline class. this Would Look Like this:

Class ccolorline: Public Cline

{

PUBLIC:

Void Draw (Long Color);

}

What's going on here? Well with this class we have all the functionality of out other class, but now we can users ot} function which allows us to set the color. The CPP Code Would Look Like:

Void ccolorline :: Draw (long color)

{

// psuedo code here, these Are Operating System

// Functions to draw a line

SetColor (Color);

Cline :: DRAW ();

Return;

}

Now we have all the functionality of the other class but we added an extra function called Draw. But it's the same name as our other Draw! No matter. Cpp is smart enough to know that if you call Draw (color) to use the new function, but if you call Draw () it will use the old function. The strange part of the code may be CLine :: Draw (). This just tells our program to call the base class's Draw function. We save ourselves from having to Write That Lineto and Moveto Code Again. Pretty Cool, Huh? Now We Can do Something Like 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'm leaving out a ton of aspects and things here. Like defining operators, overriding functions, virtual functions, protected and private members ... the list goes on. You have enough to get started though.

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

New Post(0)