Actually, I'm at home in java, so please forbear with me. Having this MWE:
#include <iostream>
using namespace std;
class MyClass {
private:
double calculated;
public:
MyClass(double x=0) : calculated(42*x) {};
void print() {
cout << "calculated=" << calculated << endl;
};
void printManipulated() {
MyClass obj;
obj.calculated = 74574; // why is this possible?
obj.print();
};
};
int main() {
MyClass obj;
obj.printManipulated();
return 0;
}
The snippet works and the console prints
calculated=74574
But why there are no errors? Why can I access the variable calculated
in the method printManipulated
unless it is declared private?