+ 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(); }

18th Feb 2017, 12:46 PM
Black Temple
Black Temple - avatar
1 ответ
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.
18th Feb 2017, 2:39 PM
Milan Todorovic
Milan Todorovic - avatar