fork download
  1. #include <iostream>
  2.  
  3. class abc{
  4. public:
  5. int a, b;
  6.  
  7. abc()
  8. { std::cout << "Default constructor\n"; a = 0; b = 0;}
  9.  
  10. abc(int x)
  11. { std::cout << "Int constructor\n"; a = x;}
  12.  
  13. abc(abc const& other): a(other.a), b(other.b)
  14. { std::cout << "Copy constructor (" << a << ", " << b << ")\n"; }
  15.  
  16. abc& operator=(abc const& other) {
  17. std::cout << "Assignment operator (" << a << ", " << b << ") = (" << other.a << ", " << other.b << ")\n";
  18. a = other.a;
  19. b = other.b;
  20. return *this;
  21. }
  22.  
  23. ~abc()
  24. {std::cout << "Destructor Called\n";}
  25. };
  26. int main()
  27. {
  28. abc obj1;
  29. std::cout << "OBJ1 " << obj1.a << "..." << obj1.b << "\n";
  30. abc obj2;
  31. std::cout << "OBJ2 " << obj2.a << "..." << obj2.b << "\n";
  32. obj2 = 100;
  33. std::cout << "OBJ2 " << obj2.a << "\n";
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 2728KB
stdin
Standard input is empty
stdout
Default constructor
OBJ1 0...0
Default constructor
OBJ2 0...0
Int constructor
Assignment operator (0, 0) = (100, 0)
Destructor Called
OBJ2 100
Destructor Called
Destructor Called