C++ 文件布局
3. 自动售票机例子
TicketMachine.h
#ifndef TICKETMACHINE_H_
#define TICKETMACHINE_H_
class TicketMachine {
public:
TicketMachine();
virtual ~TicketMachine();
void showPrompt();
void insertMoney(int money);
void showBalance();
void printTicket();
void showTotal();
private:
const int PRICE;
int balance;
int total;
};
#endif
TicketMachine.cpp
#include <iostream>
#include "TicketMachine.h"
TicketMachine::TicketMachine() : PRICE(0) {
}
TicketMachine::~TicketMachine() {
}
void TicketMachine::showPrompt() {
std::cout << "prompt: " << std::endl;
}
void TicketMachine::insertMoney(int money) {
balance = 0;
balance += money;
}
void TicketMachine::showBalance() {
std::cout << "balance: " << balance << std::endl;
}
int main()
{
TicketMachine tm;
tm.insertMoney(100);
tm.showBalance();
return 0;
}
3.1 :: 解析符(resolver)
-
<Class Name>::<function name>
-
::<function name>
-
demo.cpp
void S::f() {
::f(); // 会递归调用自己
::a++; // 默认使用全局的 a
a--; // The a at class scope
}
3.2 定义类
- In C++, seperated
.h
and.cpp
files are used to define one class. - Class declaration and prototypes in that class are in the header file(
.h
). - All the bodies of these functions are in the source file(
.cpp
).
3.2.1 头文件
- 函数:If a function is declared in a header file, you must include the header file everywhere the function is used and where the function is defined.
- 类:If a class is declared in a header file, you must include the header file everywhere the class is used and where class member functions are defined.
3.2.2 头文件 = 接口(Header = interface)
- The header is a contract between you and the user of your code.
- The compile(编译器) enforces the contract by requiring you to declare all structures and functions before they are used.
3.2.3 C++ 代码组织结构
a.h
a.cpp
include
: 是把a.h
的内容插入到a.cpp
a.h
void f();
int global;
a.cpp
#include "a.h"
int main()
{
return 0;
}
3.2.3.1 查看预编译结果
-
第一种方式:
cpp a.cpp
,会将结果直接打印在控制台; -
第二种方式:
g++ a.cpp --save-temps
,然后,使用more a.ii
,查看a.ii
文件内容 -
a.ii
# 1 "a.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "a.cpp"
# 1 "a.h" 1
void f();
int global;
# 2 "a.cpp" 2
int main()
{
return 0;
}
3.2.3.2 声明(declaration)和定义(definition)
b.cpp
#include "a.h"
void f(){
}
1,共同编译 a.cpp
和b.cpp
: g++ a.cpp b.cpp
2,出现异常信息:
3,a.h
文件中int global;
是定义,可以增加关键字“extern”,由“definition”调整为“declaration”
a.h
void f();
extern int global;
4,调整b.cpp
b.cpp
#include "a.h"
void f(){
global++;
}
5,编译:g++ a.cpp b.cpp
,出现异常:
6,继续调整b.cpp
b.cpp
#include "a.h"
int global;
void f(){
global++;
}
3.2.4 Declarations vs. Definitions
- A
.cpp
file is a compile unit. - Only declarations are allowed to be in
.h
- extern variables
- function prototypes
- class/struct declaration
3.2.5 #include
#include
is to insert the included file into the.cpp
file at where the#include
statement is.#include "xx.h"
: first search in the current directory, then the directories declared somewhere.#include <xx.h>
: search in the specified directories.#include <xx>
: same as#include <xx.h>
3.2.6 标准 .h
文件结构
- 建议:
- One class declaration per header file.
- Associated with one source file in the same prefix of file name.
- The contents of a header file is surrounded with
#ifndef #define #endif
.
#ifndef HEADER_FLAG
#define HEADER_FLAG
// Type declaration here ...
#endif // HEADER_FLAG
参考资料: