我试图在私有变量的单独文件中创建一个类.到目前为止,我的类代码是:
在TestClass.h中
#ifndef TESTCLASS_H
#define TESTCLASS_H
#include <string>
using namespace std;
class TestClass
{
private:
string hi;
public:
TestClass(string x);
void set(string x);
void print(int x);
};
#endif
Run Code Online (Sandbox Code Playgroud)
在TestClass.cpp中
#include "TestClass.h"
#include <iostream>
#include <string>
using namespace std;
TestClass::TestClass(string x)
{
cout << "constuct " << x << endl;
}
void set(string x){
hi = x;
}
void print(int x){
if(x == 2)
cout << hi << " x = two\n";
else if(x < -10)
cout << hi << " …Run Code Online (Sandbox Code Playgroud)