VC++/MFC 教程2(英文)

原创 2001年06月04日 11:22:00

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 won't 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 would define the class as follows:

class CLine 
{ 
 int m_nX1; 
 int m_nY1; 
 int m_nX2; 
 int m_nY2; 

public: 
 
 CLine(); 
 CLine(int x1, int y1, int x2, int y2); 

 
 ~CLine(); 

 
 void SetPoints(int x1, int y1, int x2, int y2); 

 
 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. Don't 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 assume this is a member variable of a class that points (it is a pointer) to a point. If you see fData, you would assume that it is a floating-point 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:

CLine MyLine; 


CLine *pMyLine; 


pMyLine = new CLine; 


CLine *pMyLine; 

pMyLine = new CLine(0,0,10,10); 


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 other data types. I could allocate an array of 100 integers with:

int *pNumbers; 


pNumbers = new int[100]; 


pNumbers[0]=0; 


pNumbers[99]=99; 


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 don't have a return value. This is the case for destructors also. A destructor is the function which automagically gets called when our CLine is deleted or goes out of scope. For instance:

CLine *pMyLine; 


pMyLine = new CLine; 


delete pMyLine; 

{ 
 
 CLine MyLine; 
} 



For our class, ~CLine() doesn't 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() 
{ 
 
} 

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() 
{ 
 
 
 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 without.

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 own class. This is done a ton in Visual C. Lets say you wanted to draw lines in your program, and you thought the line class might be nice, but it is missing an important feature, it doesn't let you set the line color. Instead of writing 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 our other class, but now we can use this other Draw() function which allows us to set the color. The CPP code would look like:

void CColorLine::Draw(long color) 
{ 
 
 
 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); 


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.

VC++ MFC入门简明教程,我当时就是看了它一天入门的

虽然有些夸张,但是这是我看到的最条理和精悍的入门级文档。我由于有一点C++和Windows开发基础,当时真的立刻明白了许多,(入门了)。每个开始学VC的人最大的问题就是总得看数百页才能有点眉目,这个文...
  • breath
  • breath
  • 2000年12月29日 00:51
  • 4542

VC++/MFC 教程1(英文)

Lesson 1: Behind the Scenes with Handles and MessagesThough you think you want to dive right into th...
  • jingqiu
  • jingqiu
  • 2001年06月04日 11:10
  • 532

VC++/MFC 教程3(英文)

Lesson 3: Visual C++ IDE and WorkspacesWindows programming is tricky stuff. Dont let anyone fool you...
  • jingqiu
  • jingqiu
  • 2001年06月04日 11:25
  • 635

VC++/MFC 教程6&7(英文)

Lesson 6: SDI and MDI ApplicationsWe are getting to some advanced stuff now. In this lesson I am not...
  • jingqiu
  • jingqiu
  • 2001年06月04日 11:36
  • 830

VC++/MFC 教程4&5(英文)

Lesson 4: MFC BasicsAre you ready to start programming? No you are not. You dont want me to teach yo...
  • jingqiu
  • jingqiu
  • 2001年06月04日 11:31
  • 789

经典C、C++、MFC、VC++教程,都是pdf完整版的

发现一个“新浪微盘”,空间大,下载方便,也没有广告。以后我会陆续把我的所有开发资料移到“新浪微盘”中,免费分享给大家。 (1) 有“新浪微博”的可以直接拷贝资料到你自己的新浪微盘(我的新浪微盘 ...
  • xyw_Eliot
  • xyw_Eliot
  • 2013年06月19日 21:39
  • 4398

贡献15本经典C、C++、MFC、VC++教程,都是pdf完整版的

发现一个“新浪微盘”,空间大,下载方便,也没有广告。以后我会陆续把我的所有开发资料移到“新浪微盘”中,免费分享给大家。 (1) 有“新浪微博”的可以直接拷贝资料到你自己的新浪微盘(我的新浪微盘 );...
  • tsroad
  • tsroad
  • 2015年11月09日 16:19
  • 4390

VC浏览器相关的学习(一)(Single document界面)

1 MFC建立一个Single document    第一步:Document/View architecture support要打勾,否则第六步不可选    第六步:BaseClass改为CHt...
  • kira8dao7
  • kira8dao7
  • 2010年08月16日 12:00
  • 2118

《VC++深入详解》孙鑫经典VC++视频教程

这里推荐孙鑫的视频,这部视频在《深入详解VC++》这本书的配套光盘里,个人觉得是最经典的不可多得的国人讲解的超高水平的VC++学习视频。视频共分20课,平均每课两个多小时。本套视频由孙鑫老师亲自授课录...
  • rainbow203
  • rainbow203
  • 2012年11月03日 15:12
  • 25896

终于找到了一篇比较全的bcg教程博文

点击打开链接
  • boxer2011111
  • boxer2011111
  • 2012年01月03日 13:23
  • 1696
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:VC++/MFC 教程2(英文)
举报原因:
原因补充:

(最多只允许输入30个字)