Beruflich Dokumente
Kultur Dokumente
Constructor
Is
used to initialize the data member of the class
Analogy in variable:
Define the variable and assign value to it
int Total ;
Total = 0;
Define the variable and initialize it (the same as
using constructor)
int Total = 0;
2
PBO II. Constructor & Destructor
Creating Constructor
3
PBO II. Constructor & Destructor
Destructor
Used to remove data in memory that has been
set using the keyword new. (For reference
variable)
As constructor, the destructor can’t have an
output data type
Destructor name must be the same as the class
name and must be preceded with “~” (tilde)
Destructor will be called automatically when the
objects are out of scope
8
PBO II. Constructor & Destructor
class Tcar{
public:
char* _Name;
E int _Gazoline_in_tank;
X Tcar(){
A int length = strlen("Undefined Car");
_Name = new char[length + 1];
M strcpy(_Name, "Undefined Car");
P }
L //-Destructor-------------------------
~Tcar(){
E delete []_Name;
}
}; 9
PBO II. Constructor & Destructor
Memory Location for Object
Object created Memory when
Constructor is Memory when object destroyed
called object created without destructor
void main() Car2._GIT = 0 10 removed
{ Car2._Name = 100 12 removed
Tcar Car1;
…
:
}
100
“Undefined Car” “Undefined Car”
Object out of
scope in here,
destructor will
be called
automatically 10
PBO II. Constructor & Destructor
Memory Loc (cont.)
Memory when
object destroyed
with destructor Note: When the data are removed,
10 removed the content of the memory usually is
kept intact (still the same as before)
12 removed but the location actually is offered to
other data. Therefore reading the
content of this memory right away
after deleting, may give you the same
result (and give a false perception as if
100 your data still there). Worse case may
Removed !! happen if programmer altering the
content of the freed memory. This
action may crashing your computer
system (Hang).
11
PBO II. Constructor & Destructor
12
PBO II. Constructor & Destructor
Copy Constructor
What happen when you assign another object to a
new object such as:
…
Assign Car 2 from Car 1
Tcar Car2 = Car1;
…
Or passing the object as an argument:
Car2.take_gazoline_from(Car1);
:
100
Compiler will perform: “Undefined Car”
Car2._GIT = Car1._GIT
Car2._Name = Car1._Name
14
PBO II. Constructor & Destructor
15
PBO II. Constructor & Destructor
16
PBO II. Constructor & Destructor
:
100
“Undefined Car”
150
“Undefined Car”
18
PBO II. Constructor & Destructor
Characteristic of
Copy Constructor Copy Constructor
Tcar(const Tcar &temp_car){
_Gazoline_in_tank = temp_car._Gazoline_in_tank;
Allocating new memory location like this to place the different data,
often called DEEP COPY (as an opposed to SHALLOW COPY)
Deep copy will be automatically performed on :
Tcar Car2 = Car1; 19
PBO II. Constructor & Destructor
Conclusion
If you have a POINTER data member then
Always create a destructor to delete the data
allocated using the keyword new
Always create the copy constructor to perform deep
copy
21
PBO II. Constructor & Destructor
Exercise (Take home)
Main :
22
PBO II. Constructor & Destructor