本关有一个未完成的类Int,该类含有一个成员变量,请为该成员变量编写存取函数。存取函数以GoogleC++风格命名,具体请参考主函数中的代码。
一共包含2个文件,main.cpp和Int.h。 用户仅能修改Int.h中的内容。 main.cpp的内容如下:
#include "Int.h"
#include
using namespace std;
int main(){
int x;
cin>>x;
Int a;
a.setValue(x);
cout<<a.getValue()<<endl;
return 0;
}
然后编辑页面为
#ifndef INT_H //这是define guard
#define INT_H //在C和C++中,头文件都应该有这玩意
class Int{
#include <iostream>
using namespace std;
class Int
{
public:
constexpr int getValue() const { return _value; }
constexpr void setValue(int value) { _value = value; }
private:
int _value = 0;
};
int main()
{
int x;
cin >> x;
Int a;
a.setValue(x);
cout << a.getValue() << endl;
return 0;
}