我在单个 cpp 文件 ( tested.cpp
) 中有以下代码:
class tested {
private:
int x;
public:
tested(int x_inp) {
x = x_inp;
}
int getValue() {
return x;
}
};
现在我想为这段代码写一个头文件。它应该怎么看?有了头文件后,我应该在我的 cpp 文件中更改什么。我想我的头文件应该是这样的:
class tested {
private:
int x;
public:
tested(int x);
int getValue();
}
然后在我的 cpp 文件中我应该#include "tested.h"
. 我还需要通过以下方式替换整个班级:
tested::tested(int c_inp) {
x = x_inp;
}
tested::getValue(){
return x;
}
这样对吗?