+ 1
Why can we not print the valur of 'var' without obj.printinfo function.? What is the role of this function?
#include <iostream> using namespace std; class MyClass { public: MyClass(int a) : var(a) { } void printInfo() { cout << var <<endl; cout << this->var <<endl; cout << (*this).var <<endl; } private: int var; }; int main() { MyClass obj(42); obj.printInfo(); }
1 Resposta
0
Because we marked var to be private. That means we can access it like obj.var. We have to use some method from within the object.