Guest User

Untitled

a guest
Jul 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. class Kotak {
  4. public:
  5. Kotak(int id) : _id(id) {
  6. printf("[%d] constructor\n", _id);
  7. }
  8. ~Kotak() {
  9. printf("[%d] destructor\n", _id);
  10. }
  11. void siapa() {
  12. printf("[%d] ini kotak\n", _id);
  13. }
  14. private:
  15. int _id;
  16. };
  17.  
  18. int main() {
  19. Kotak a(1);
  20. a.siapa();
  21.  
  22. {
  23. Kotak b(2);
  24. }
  25.  
  26. Kotak c(3);
  27. c.siapa();
  28.  
  29. Kotak *d = new Kotak(4);
  30. d->siapa();
  31. delete d;
  32.  
  33. {
  34. Kotak e(5);
  35. d = &e;
  36. d->siapa();
  37. }
  38.  
  39. d->siapa();
  40. }
  41.  
  42. /* output:
  43.  
  44. [1] constructor
  45. [1] ini kotak
  46. [2] constructor
  47. [2] destructor
  48. [3] constructor
  49. [3] ini kotak
  50. [4] constructor
  51. [4] ini kotak
  52. [4] destructor
  53. [5] constructor
  54. [5] ini kotak
  55. [5] destructor
  56. [5] ini kotak
  57. [3] destructor
  58. [1] destructor
  59.  
  60. */
Add Comment
Please, Sign In to add comment