即時創建類,所以我可以更好地理解它們。當我創建一個類應該使用頭文件和cpp?C++使用標題和cpp文件創建類
0
A
回答
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());
}
相關問題
- 1. 使用nmake從cpp文件創建dll
- 2. Makefile構建C和CPP文件
- 3. 使用.NET(C#)創建Excel文件 - 列標題出現問題!
- 4. C++創建和使用類
- 5. 標題和CPP或只是CPP文件 - 最佳實踐?
- 6. C++創建在單獨的.cpp和.h文件
- 7. 使用.cpp和.c?
- 8. 使用在CPP文件創建爲一個頭文件
- 9. 創建XML文件標題
- 10. 使用cpp,python和csv文件創建一個可執行文件?
- 11. 聲明頭文件和cpp文件C++
- 12. 如何在android studio中創建一個C++類(.h和.cpp)?
- 13. 爲什麼在創建新的C++類時創建了兩個文件(.h和.cpp)?
- 14. 使用Xcode調試C++ .cpp文件
- 15. 從C/cpp文件
- 16. CPP | .h文件(C++)
- 17. 爲什麼class在使用類名相同的頭文件和cpp文件創建時出現錯誤
- 18. 在多個cpp文件中使用類/結構體/聯合C++
- 19. PHP - 使用woocommerce REST api(V3)問題創建類別和標籤
- 20. 製作和C++文件, 「包括」 在CPP
- 21. C++ .h和.cpp文件分開
- 22. Xcode中的Objective-C++和.cpp文件
- 23. C++:分離標題和實現文件(.hpp/.cpp)時出現分段錯誤
- 24. 使用C++庫,正值的.cpp和.h文件
- 25. 如何在cpp和cuda模塊中使用cuda類頭文件
- 26. 使用XML和C創建Word文檔#
- 27. 的C/C++項目(頭文件和cpp文件)
- 28. ASP.NET MVC。如何使用DisplayNameFor來創建表標題和正文?
- 29. 爲CSV文件創建標題
- 30. 爲什麼使用Prolog和Lisp來創建cpp \ C \ C#來創建人工智能?
的[幫助一個C++新手明白了自己的錯誤可能重複文件](http://stackoverflow.com/questions/1686204/help-ac-newbie-understand-his-mistakes-header-files-and-cpp-files) – 2010-11-04 22:23:22