我仍然在努力学习类,但是我创建了这个,我似乎得到了一些奇怪的数字,如内存号或某些东西,我无法弄清楚什么是错的。class not working properly
#include <iostream>
#include <string>
using namespace std;
class JoesClass {
public:
int setAge(int age) {
return age;
}
void displayAge() {
cout << "your age is " << age << endl;
}
private:
int age;
};
int main()
{
JoesClass newobject;
newobject.setAge(40);
newobject.displayAge();
return 0;
}
好的,谢谢,我现在看到它,我只是将函数中的变量更改为X. – user3275730
为什么你建议使用'age = new_age;'而不是'this-> age = age;'?我发现后者更易于阅读,因为它更加明确。 – user2079303
将名称改为比'X'更具描述性的名称。如果你不使用'this-> age = age;' – bblincoe