Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. using namespace std;
  2.  
  3. #include <iostream>
  4. static int count;
  5.  
  6. class A{
  7.  
  8. public:
  9. int c;
  10. A(){c = ++count; cout << "A() " << c << endl;}
  11. A(int j){c = ++count; cout << "A(int) " << c << endl;}
  12. ~A(){cout << "~A() " << c << endl;}
  13.  
  14. };
  15.  
  16. class B{
  17. public:
  18. A a;
  19. B(){
  20. cout << "This is B's constructor" << endl;
  21. A a(); //This is the important line.
  22. }
  23. };
  24.  
  25. int main(){
  26. B b;
  27. cout << "The object we keep is " << b.a.c << endl;
  28. }
  29.  
  30. A() 1
  31. This is B's constructor
  32. The object we keep is 1
  33. ~A() 1
  34.  
  35. A() 1
  36. This is B's constructor
  37. A(int) 2
  38. ~A() 2
  39. The object we keep is 2
  40. ~A() 2
  41.  
  42. A a(); //This is the important line.
  43.  
  44. A a{}; // *this* is a variable declaration
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement