fork(3) download
  1. #include <iostream>
  2.  
  3. class Example {
  4. private:
  5. int a;
  6. // lots of important variables that live on the heap
  7. public:
  8. int getA() { return a; }
  9.  
  10. // Example() = default;
  11. Example() {
  12. std::cout << "default constructor" << std::endl;
  13. a = 0;
  14. }
  15.  
  16. Example(int a_local) {
  17. a = a_local;
  18. std::cout << "custom constructor" << std::endl;
  19. /* allocating memory, etc. ... */
  20. }
  21. };
  22.  
  23. int main() {
  24. std::cout << "Running default constructor" << std::endl;
  25. Example e1;
  26. std::cout << "e1.a is " << e1.getA() << std::endl;
  27.  
  28. std::cout << "Running custom constructor" << std::endl;
  29. Example e2(500);
  30. std::cout << "e2.a is " << e2.getA() << std::endl;
  31. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
Running default constructor
default constructor
e1.a is 0
Running custom constructor
custom constructor
e2.a is 500