小编Mr.*_*ums的帖子

C++ - 类中的私有变量

我试图在私有变量的单独文件中创建一个类.到目前为止,我的类代码是:

在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)

c++ private class

9
推荐指数
2
解决办法
5万
查看次数

标签 统计

c++ ×1

class ×1

private ×1