xidos
Paywall Strategist
Divine
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:
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
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:
- // Definition of class CreateAndDestroy.
- class
CreateAndDestroy
- {
- public
:
- CreateAndDestroy(
int
value)
// constructor
- {
- data =
value;
- cout
<<
"Object "
<<
data <<
" constructor"
;
- }
- ~CreateAndDestroy(
)
// destructor
- {
- cout
<<
"Object "
<<
data <<
" destructor "
<<
endl;
}
- }
- private
:
- int
data;
- }
;
- void
create(
void
)
;
// prototype
- CreateAndDestroy first(
1
)
;
// global object
- int
main(
)
- {
- cout
<<
" (global created before main)"
<<
endl;
- CreateAndDestroy second(
2
)
;
// local object
- cout
<<
" (local in main)"
<<
endl;
- create(
)
;
// call function to create objects
- CreateAndDestroy fourth(
4
)
;
// local object
- cout
<<
" (local in main)"
<<
endl;
- return
0
;
- }
- // Function to create objects
- void
create(
void
)
- {
- CreateAndDestroy fifth(
5
)
;
- cout
<<
" (local in create)"
<<
endl;
- CreateAndDestroy seventh(
7
)
;
- cout
<<
" (local in create)"
<<
endl;
- }
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.