fork(2) download
  1. #include <iostream>
  2.  
  3. class Example {
  4. private:
  5. int a;
  6. public:
  7. int getA() { return a; }
  8.  
  9. // Example class is immutable unless we add a setter method
  10. // bool setA(int a2) {
  11. // if (a2 < 100 && a2 >= 0) {
  12. // a = a2;
  13. // return true;
  14. // } else {
  15. // return false;
  16. // }
  17. // }
  18. };
  19.  
  20. int main() {
  21. Example e;
  22.  
  23. std::cout << "e.a is " << e.a << std::endl;
  24.  
  25. e.a = 10;
  26.  
  27. // int n = -10;
  28. // if (e.setA(n)) {
  29. // std::cout << "e.a is now " << e.getA() << std::endl;
  30. // } else {
  31. // std::cout << "Could not set e.a to " << n << std::endl;
  32. // }
  33. }
Compilation error #stdin compilation error #stdout 0s 15232KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:23:30: error: ‘int Example::a’ is private within this context
  std::cout << "e.a is " << e.a << std::endl;
                              ^
prog.cpp:5:13: note: declared private here
         int a;
             ^
prog.cpp:25:4: error: ‘int Example::a’ is private within this context
  e.a = 10;
    ^
prog.cpp:5:13: note: declared private here
         int a;
             ^
stdout
Standard output is empty