fork download
  1. #include <iostream>
  2.  
  3. class Widget
  4. {
  5. public:
  6. Widget(): x(0) {} // constructor with initialization list
  7. void setX(int newVal) { x = newVal; } // changes the state of an instance
  8. void printX() { std::cout << x << std::endl; } // interacts with the state of an instance
  9.  
  10. static void printClassName() { std::cout << "Widget" << std::endl; } // doest change or interact with the state therefore can be made static
  11. private:
  12. int x;
  13. };
  14.  
  15. int main(int argc, char* argv[])
  16. {
  17. Widget w;
  18. w.printX();
  19. w.setX(4);
  20. w.printX();
  21. Widget::printClassName();
  22. //w::printX(); <-- this won't compile because it is not static
  23.  
  24. return 0;
  25. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
0
4
Widget