Visual C++MFC 指南(2):C++要点

    技术2022-05-11  132

     

     

      Visual C++/MFC 指南 第二课: C++ 要点  

    如果你想使用Microsoft Visual C++,懂得C++中关于类的内容将会有极大的帮助。如果你习惯使用简单的C,你只有实践过才能掌握对类的处理。在开始VC++之前,让我们来复习一下你应该弄清楚的关于类的内容。

    在很大程度上来说类是一种结构。我们从一个例子入手来而不是仅说明规则。写一个类来描述直线。在.h文件里这样定义类:

    class CLine {  int m_nX1;  int m_nY1;  int m_nX2;  int m_nY2;   public:  // constructors  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(); } ;

            简短的说一下命名惯例。类的名字通常是由‘C’打头;成员变量使用前缀m_’,接着按照微软的习惯使用一个字母来指明数据类型,然后是变量的名称。所有的单词用大写开头。我推荐这种微软的标准(叫做匈牙利法),因为它使用广泛而且容易看懂。这样的话以后你看到m_pPoint,你就会想到这是一个成员变量而且是指向POINT类型的指针;看到fData就会想到这是一个浮点值。

            回到关于类的讨论。整型变量记录线的端点。注意它们是放在‘public:’之前的,表明使用这个类的程序员不能直接使用这些变量,它们不是“公开”使用的。那些在公有声明下面的函数是公开使用的。前面的两个叫做构造函数,这些函数总是在一个新的Cline对象被建立的时候执行。下面是它们被调用的一些时候:

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

    所有的这些都建立了一条直线。有的直线被初始化为默认的设置有的则用了新的参数。关键字“new”在C++里建立新的对象,类似于C里的malloc。你必须对使用“new”的所有对象使用“delete”,就像在c里用free。不仅对类是这样,其他的数据类型也一样。我分配一个有100个整型数据的数组:

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

            注意在delete后面的[],这是在对程序说删除整个数组。如果你写的是“delete pnumbers;”,就只删除了第一个元素。这样就会造成内存泄漏。

        对不起啊,让我们回到Cline的构造函数。一条直线在建立的时候自动条用构造函数,代码是这样的:

    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;

    }

             我们看到,除了把类名和两个冒号(CLine::)放在函数名的前面.,函数的声明很像标准的C函数。一个差异是构造函数没有返回值,析构函数也是如此。析构函数是在我们的Cline对象被删除或出了生存空间后被自动调用的。比如:

    // 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')

             对于我们这个类,~Cline()不必做任何事情。但你可以把做清理的代码放在这里,象回收类中分配的内存。以为现在不必清理所以函数是空的:

    CLine::~CLine() {  // do nothing }

    现在我们来填写另外的两个函数:

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

    怎么调用这些它们呢?这里有两个例子。 一个使用了指针另一个没有用指针:

    CLine *pLine = new CLine(0,0,10,10); pLine->Draw(); delete pLine; CLine MyLine; MyLine.SetPoints(0,0,10,10); MyLine.Draw();

    这个类就完成了。现在这个类可以在别的类里。你可以用4个直线来建立一个正方形类CSquare:

    class CSquare {  CLine m_LineTop;  CLine m_LineLeft;  CLine m_LineBottom;  CLine m_LineRight;  //... }

    还有更好的,根据类的特性,你可以用Cline类来建立你自己的类。这个用法在VC里用的太多了。比如你想在程序里画直线,那你就会想用直线类就好了,但这样做还缺少一个重要的特性,不能设置直线的颜色。当然不用再来写新的类了,更简单的办法是继承Cline类。像这样:

    class CColorLine : public CLine { public:  void Draw(long color); };

    现在怎么样了呢?这个类有Cline类的所有功能,而且我们可以使用另外一个可以设置颜色的Draw()函数,在CPP文件里代码是这样的:

    void CColorLine::Draw(long color) {  // psuedo code here, these are operating system  // functions to draw a line  SetColor(color);  CLine::Draw();  return; }

    现在我们有了另外一个类的上所有功能而且添加了一个额外的Draw函数。但这个函数跟原来的Draw函数是同名的!没关系,C++足够聪明,它能辩明:如果你调用Draw(color)就使用新的函数而如果你调用Draw()就用旧的函数。在代码里CLine::Draw()也许会使你感到陌生。这是在告诉程序调用基类的画线函数。我们不必在费时重写LineTo MoveTo的代码了,很好,不是么?现在我们可以这样做了:

    CColorLine MyLine; MyLine.SetPoints(0,0,10,10); // assuming 0 is black, this will draw a black line. MyLine.Draw(0);

    当然,这里我省略了很多方面的问题。比如定义操作符,函数重载,虚函数,保护和私有成员……等等。但已经足够让你继续了。


    最新回复(0)