.h):類定義、成員聲明。.cpp):成員函數實現,#include "ClassName.h"。main.cpp):包含所需頭文件。File → New → C++ Header/File,或.h/.cpp,再 Project → Add To Project → Files 添加。

main.cpp 包含 animal.h 和 fish.h,而 fish.h 又包含 animal.h → animal 類重複定義錯誤。

// animal.h
#ifndef ANIMAL_H_H
#define ANIMAL_H_H
class animal {
public:
animal(); ~animal();
void eat(); void sleep();
virtual void breathe();
};
#endif
// fish.h
#ifndef FISH_H_H
#define FISH_H_H
#include "animal.h"
class fish : public animal {
public:
void breathe();
};
#endif
PROJECT_MODULE_H),避免跨項目衝突。iostream 等)內部已有守衛,重複包含無害。默認隱藏已知擴展名,重命名 test.txt → test.cpp 實際變為 test.cpp.txt。需在「資源管理器 → 工具 → 文件夾選項 → 查看」取消「隱藏已知文件類型的擴展名」。