• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Object-Oriented Programming In C++: Destructor

xidos

Paywall Strategist
Divine
X Rep
0
0
0
Rep
0
X Vouches
0
0
0
Vouches
0
Posts
163
Likes
115
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
Contents:

1. Destructor.

2. When Constructors and Destructors are Called.

Destructor

Is a member function in the class, which has the following characteristics:
1. It has the same name as the class preceded with a tilde '~'. For example, the destructor for class Time is declared:
~Time(

)

2. It doesn't return any type.
3. It is called when objects are destroyed to release all allocated resources.
4. There is just one destructor in a class, it can not be overloaded.
When Constructors and Destructors are Called:

They are called dynamically and that depends on the scope of objects.
Global Scope Objects:

The constructor is called dynamically before any other function even the main function, and the destructor is called dynamically at the end of the main function.
Local Scope Objects:

The constructor is called dynamically when the object is defined, and the destructor is called dynamically at the end of the block of statements where the object is defined.
Example:
  1. // Definition of class CreateAndDestroy.
  2. class

    CreateAndDestroy
  3. {
  4. public

    :
  5. CreateAndDestroy(

    int

    value)

    // constructor
  6. {
  7. data =

    value;
  8. cout

    <<

    "Object "

    <<

    data <<

    " constructor"

    ;
  9. }
  10. ~CreateAndDestroy(

    )

    // destructor
  11. {
  12. cout

    <<

    "Object "

    <<

    data <<

    " destructor "

    <<

    endl;

    }
  13. }
  14. private

    :
  15. int

    data;
  16. }

    ;

  17. void

    create(

    void

    )

    ;

    // prototype

  18. CreateAndDestroy first(

    1

    )

    ;

    // global object

  19. int

    main(

    )
  20. {
  21. cout

    <<

    " (global created before main)"

    <<

    endl;

  22. CreateAndDestroy second(

    2

    )

    ;

    // local object
  23. cout

    <<

    " (local in main)"

    <<

    endl;

  24. create(

    )

    ;

    // call function to create objects
  25. CreateAndDestroy fourth(

    4

    )

    ;

    // local object
  26. cout

    <<

    " (local in main)"

    <<

    endl;
  27. return

    0

    ;
  28. }

  29. // Function to create objects
  30. void

    create(

    void

    )
  31. {
  32. CreateAndDestroy fifth(

    5

    )

    ;
  33. cout

    <<

    " (local in create)"

    <<

    endl;

  34. CreateAndDestroy seventh(

    7

    )

    ;
  35. cout

    <<

    " (local in create)"

    <<

    endl;
  36. }

The Output:
Object 1 constructor (global created before main)
Object 2 constructor (local in main)
Object 5 constructor (local in create)
Object 7 constructor (local in create)
Object 7 destructor
Object 5 destructor
Object 4 constructor (local in main)
Object 4 destructor
Object 2 destructor
Object 1 destructor

Note: You can find the full source code of this example in code.zip file.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,499

349,752

349,762

Top