2010-11-04 46 views
0

即時創建類,所以我可以更好地理解它們。當我創建一個類應該使用頭文件和cpp?C++使用標題和cpp文件創建類

+0

的[幫助一個C++新手明白了自己的錯誤可能重複文件](http://stackoverflow.com/questions/1686204/help-ac-newbie-understand-his-mistakes-header-files-and-cpp-files) – 2010-11-04 22:23:22

回答

3

頭文件用於類定義和實現的cpp文件。事情是這樣的:

Test.h

class Test 
{ 
public: 

    void PrintHelloWorld(void); 
}; 

Test.cpp的

void Test::PrintHelloWorld(void) 
{ 
    cout << "Hej på dig världen!"; 
} 
0

這是一個非常基本的問題。你應該可以找到一個C++教程或書籍來幫助你。

頭文件具有類定義。

cpp文件具有類實現。

0

頭文件應包括 「的#include」 指令,常量定義和類方法的定義:構造函數,析構函數和方法。想想你喜歡你定義一個類的接口。

Cpp應該包含實現。所以在cpp中,你意識到你在.h文件中聲明的所有內容。

下面是一個例子:

CBug.h:

#ifndef CBUG_H 
#define CBUG_H 

#include <string> 
#include <vector> 

using namespace std; 

enum BugState 
{ 
    ILLEGAL = -1, 
    ACTIVE = 0, 
    RESOLVED, 
    CLOSED  
}; 

/** 
* CBug - class to describe the record about any bug 
*/ 
class CBug 
{ 
    private: 
     string DevName; 
     string Project; 
     int Id; 
     string ErrorDesc; 
     BugState State; 
     // class constructor 
     CBug(); 


    public: 

    // class destructor 
    ~CBug(); 

    /** 
    * CreateBug 
    * creates a new bug, reads values from string and returns a pointer to a bug 
    * @param Params contains all necessary information about bug 
    * @return pointer to a newly created bug 
    */ 
    static CBug * CreateBug (string Params); 
     // setters 
     void SetState(BugState); 

     //getters 
     string GetDeveloper(); 
     int GetId(); 
     int GetState(); 
     bool IsActive(); 
     string ToString(); 
}; 

#endif // CBUG_H 

CBug.cpp:頭文件和CPP:

// Class automatically generated by Dev-C++ New Class wizard 

#include "CBug.h" // class's header file 
#include "CUtil.h" 

#include <iostream> 
#include <sstream> 

// class constructor 
CBug::CBug() 
{ 

} 

// class destructor 
CBug::~CBug() 
{ 

} 

CBug * CBug::CreateBug (string Params) 
{ 
#if 1  
    cout << "Param string:" << Params << endl; 
#endif 
    if(Params.length() == 0) { 
     return NULL;  
    } 

    CBug * Bug = new CBug(); 

    if (Bug != NULL) 
    { 
     vector<string> s(5); 
     s = CUtil::StringSplit (Params, " "); // разбиваем строку с параметрами на отдельные строки 
     cout << s[0] << endl; 

     Bug->DevName = s[0]; 
     Bug->Project = s[1]; 
     Bug->ErrorDesc = s[3]; 
     sscanf(s[2].c_str(), "%d", &(Bug->Id)); 
     cout << "id: " << Bug->Id << endl; 
     Bug->State = ACTIVE; 
     } 

    return Bug; 
} 

string CBug::GetDeveloper() 
{ 
    return DevName; 
} 

int CBug::GetState() 
{ 
    return State; 
} 

int CBug::GetId() 
{ 
    return Id; 
} 

void CBug::SetState (BugState state) 
{ 
    State = state; 
} 

bool CBug::IsActive() 
{ 
    return (State!=CLOSED); 
} 

string CBug::ToString() // для вывода пользователя 
{ 
     ostringstream out; 
     out << "Id: " << Id << " DevName: " << DevName << " Project: " << Project << " Desc: " << ErrorDesc << " State: " << State; 
     return(out.str()); 
} 
+2

你應該有儘可能少的,如果有的話,#include的頭文件。 – ROAR 2010-11-04 21:40:54

+0

是的,標題應該只包含他們需要的東西,然後轉發宣告剩下的東西。這可以減少在其他位置包含該標頭時的依賴關係。 – Matt 2011-03-14 22:04:31