工程规范
File design:¶
Declaration (.h)¶
#ifndef _DATABASE_H_ #define _DATABASE_H_ #include "tuple.h" // 只需调用声明 #include "adiwriter.h" // 只需调用实际用到的子类 #include <iostream> #include <string> #include <set> class Database { private: std::set<tuple> records; // ... public: Database(); Database(std::string filename); ~Database(); // ... } #endif
- ifndef:防止嵌套调用时出现反复调用的情况。
类型定义¶
- 重载运算符:用于排序与比较。
- 类型定义后使用同struct(不如说就是一个东西)
继承类应用¶
namespace使用¶
- namespace命名空间,包含多种关键字,std为常用的命名空间,cin,cout即为其中的关键字
- 若只想使用其中一个:
using std::sort; - 在一个代码块内如果有和命名空间相同的变量名,那么使用
using namespace是无效的。 - 自定义namespace:
- 跨文件使用:同class,不过用
using namespace调用
读写¶
argc和argv:命令行参数- 第一个参数
argc,用于参数计数,其值等于命令行中参数的个数;第二个参数argv,用于参数向量,是一个指向字符串数组的指针。
下面的代码用于读入输入的命令(因为要用batch,所以这样读)
-
iostream:标准读写流 -
cin,cout分别是istream,ostream的对象,我们可以自己创造其他istream,ostream的对象。 -
freopen("test.txt","w",stdout)将标准输出重定向到test.txt。 -
fstream:文件读写流
Makefile使用¶
# Declare compiler variable CXX=g++ # Declare compilation options CXXFLAGS=-Wall -g --std=c++11 # Declare linking options LDFLAGS= # Declare source code files SRCS=main.cpp tuple.cpp writer.cpp adiwriter.cpp database.cpp # Convert source code files to a list of object files OBJS=$(SRCS:.cpp=.o) # Declare executable name TARGET=adif # Default target all: $(TARGET) # Dependencies $(TARGET): $(OBJS) $(CXX) $(LDFLAGS) -o $(TARGET) $(OBJS) # Automatically generate dependencies .depend: $(SRCS) rm -f ./.depend $(CXX) $(CXXFLAGS) -MM $^ > ./.depend # Include dependencies -include .depend # Clean clean: rm -f $(OBJS) $(TARGET) .depend
batch文件¶
-
make.bat -
窗口交互型
- 没有Makefile,用于编译与生成可执行文件Main
-
test.bat -
指令测试型
- 有Makefile,用于调用Makefile编译以及输入命令测试。
- echo为回显在屏幕上的内容
- 没有echo的为输入的指令,
mingw32-make调用Makefile - pause后,需按任意键才会输入下一条指令