admin 發表於 2015-12-23 06:38:12

Class code and header files (C++)

Defining member functions outside the class definitionAll of the classes that we have written so far have been simple enough that we have been able to implement the functions directly inside the class definition itself. For example, here’s our ubiquitous Date class:class Date
{
private:
    int m_nMonth;
    int m_nDay;
    int m_nYear;

    Date() { } // private default constructor

public:
    Date(int nMonth, int nDay, int nYear)
    {
      SetDate(nMonth, nDay, nYear);
    }

    void SetDate(int nMonth, int nDay, int nYear)
    {
      m_nMonth = nMonth;
      m_nDay = nDay;
      m_nYear = nYear;
    }

    int GetMonth() { return m_nMonth; }
    int GetDay(){ return m_nDay; }
    int GetYear() { return m_nYear; }
};as classes get longer and more complicated, mixing the definition and the implementation details makes the class harder to manage and work with.C++ provides a way to separate the definition portion of the class from the implementation portion. This is done by defining the class member functions outside of the class definition. To do so, simply define the member functions of the class as if they were normal functions, but prefix the class name to the function using the scope operator   ( :: )   (same as for a namespace).class Date
{
private:
    int m_nMonth;
    int m_nDay;
    int m_nYear;

    Date() { } // private default constructor

public:
    Date(int nMonth, int nDay, int nYear);

    void SetDate(int nMonth, int nDay, int nYear);

    int GetMonth() { return m_nMonth; }
    int GetDay(){ return m_nDay; }
    int GetYear() { return m_nYear; }
};

// Date constructor
Date::Date(int nMonth, int nDay, int nYear)
{
    SetDate(nMonth, nDay, nYear);
}

// Date member function
void Date::SetDate(int nMonth, int nDay, int nYear)
{
    m_nMonth = nMonth;
    m_nDay = nDay;
    m_nYear = nYear;
}Note that the prototypes for these functions still exist inside the class definition, but the actual implementation has been moved outside:Putting class definitions in a header fileHere’s our Date class again, broken into a .cpp and .h file:Date.h:<font color="#ff0000">#ifndef DATE_H
#define DATE_H</font>

class Date
{
private:
    int m_nMonth;
    int m_nDay;
    int m_nYear;

    Date() { } // private default constructor

public:
    Date(int nMonth, int nDay, int nYear);

    void SetDate(int nMonth, int nDay, int nYear);

    int GetMonth() { return m_nMonth; }
    int GetDay(){ return m_nDay; }
    int GetYear() { return m_nYear; }
};

<font color="#ff0000">#endif</font>Date.cpp:<font color="#ff0000">#include "Date.h"</font>

// Date constructor
Date::Date(int nMonth, int nDay, int nYear)
{
    SetDate(nMonth, nDay, nYear);
}

// Date member function
void Date::SetDate(int nMonth, int nDay, int nYear)
{
    m_nMonth = nMonth;
    m_nDay = nDay;
    m_nYear = nYear;
}Now any other header or code file that wants to use the date class can simply #include "Date.h". Note that Date.cpp also needs to be compiled into any project that uses Date.h so the linker knows how Date is implemented. Don’t forget the header guards on the .h file!
頁: [1]
查看完整版本: Class code and header files (C++)