• Source
    1. #include <iostream>
    2. using namespace std;
    3.  
    4. class first {
    5. int random;
    6. public:
    7. first() {
    8. cout << "first created." << endl;
    9. }
    10. ~first() {
    11. cout << "first destroyed." << endl;
    12. }
    13. };
    14.  
    15. class composition {
    16. first* firstObj;
    17. public:
    18. composition () {
    19. firstObj = new first;
    20. if (firstObj != NULL) {
    21. cout << "firstObj points to an object of first" << endl;
    22. }
    23. cout << "An object of composition has been created." << endl;
    24. }
    25.  
    26. ~composition() {
    27. delete firstObj;
    28. cout << "composition destroyed." << endl;
    29. }
    30. };
    31.  
    32. int main() {
    33. composition com;
    34. return 0;
    35. }
    36.  
    37. /*
    38. Output:
    39.  
    40. first created.
    41. firstObj points to an object of first
    42. An object of composition has been created.
    43. first destroyed.
    44. composition destroyed.
    45.  
    46.  
    47. With delete firsObj ommited:
    48.  
    49. first created.
    50. firstObj points to an object of first
    51. An object of composition has been created.
    52. composition destroyed.
    53.  
    54. */