Cp3 3 Constructor Destructor
Cp3 3 Constructor Destructor
Cp3 3 Constructor Destructor
and Destructors
Constructor
Constructor—what’s this?
method used for initializing objects (of certain
class)
a recipe for creating object of a given class
T(args); or T::T(args);
class point
{
double x, y;
public:
// …
point(double, double); // no return value, even void
point(double); // overloadable
point();
};
Constructor—an unusual method
We cannot not specify the return value (even:
no void).
We cannot call it for the already constucted
object.
We cannot get it’s address.
It is not even visible in the class scope and has
no name (according to ANSI).
Calling
Constructor with no args (default constructor):
point p1;
Others:
or:
class point
{
double x, y;
public:
// …
};
Initialization list
inline point::point(double d)
:x(d), y(d)
{
// all done
}
point::point(double x, double y)
:x(x), y(y) // it is unambiguous !!!
{
// all done
}
Initialization list
the only way of initializing const and reference
members.
in i.l. except the class members (declared in the very
class, and not inherited) we may also define the way
of constructing virtual and direct base classes.
position on the i.l. is of no importance, initialization
is performed in a following order: base classes
(virtual and then direct bases in order of declaration),
member variables in order of declaration,
constructor’s body.
Initialization list
point::point(int x, int y):x(x), y(y) {}; //OK.
T::T()
{
}
segment::segment()
:p1(1.0, 1.0) // constr. p1 (2-arg), constr. p2 (default)
{ // number not initialized
p2=point(1.0); // constr. temp. (1 arg.), assignmemt
}; // destr. temp.
Copy constructor
it is for initializing objects using other objects of the
same class, for the T class:
T::T(const T &);
np.:
~point() // no args, no ret. value
{
cout << ”\nit’s me, The Point (x:”
<< x << „ y:” << y;
<< „) in a moment I’ll be The Late Point”;
}
Destructor
as opposed to constructor it is in the class
scope
we may call it
delete pp1;
delete pp2;
delete pp0;
delete [] arrPoints;
Example
point global=777;
int f(int) { segment o; }
void main() {
point local;
point *p_local;
local=global;
local=point(10,20);
int a=f(1);
for (int i=0; i<2; i++) {
point in_block=local;
}
p_local = new point(1.0);
point local2(10,20);
delete p_local;
p_local = new point();
}
Example
point global=777;
int f(int) { segment o; } c4, c5, d5, d4
c1
void main() {
point local; c2
point *p_local;
local=global;
local=point(10,20); c3, d3
int a=f(1);
for (int i=0; i<2; i++) {
point in_block=local; c6, i==1 c7
} d6, i==1 d7
p_local = new point(1.0); c8
point local2(10,20); c9
delete p_local; d8
p_local = new point(); c10
} d9, d2,
d1, ???10
Example
define class: person
class person
{
int age;
char *name,
*lastName;
public:
person(const char *name, const char *lastName, const int age);
person(const person & o);
~person();
};
Example
inline person::person(const char *name, const char *lastName, const int age)
:age(age)
{
person::name=new char[strlen(name) + 1];
strcpy(person::name, name);
person::lastName=new char[strlen(lastName) + 1];
strcpy(person::lastName, lastName);
}
class queue
{
person **persons; // array of pointers to persons in queue
const int capacity; // queue capacity
int length; // number of persons currently in queue
public:
queue(int capacity);
~queue();
int insert(const person &o); // 1-no more room, 0-inserted
int collect(person &o); // 1-empty queue, 0-collected
};
Example
queue::queue(int capacity)
:capacity(capacity), length(0),
persons(new (person*) [capacity])
{
}
queue::~queue()
{
for (int i=0; i<length; i++)
delete persons[i]; // elements
delete [] persons; // array
}
Example
int queue::insert(const person &o)
{
if (length==capacity)
return 1;
persons[length++]=new person(o); // copy the method’s arg.
return 0;
}
Example
int queue::collect(person &o)
{
if (length==0)
return 1;
length--;
for(int i=0; i<length; i++)
persons[i]=persons[i+1];
return 0;
}
Example
int queue::collect(person &o)
{
if (length==0)
return 1;
length--;
for(int i=0; i<length; i++)
persons[i]=persons[i+1];
return 0;
}
// find the memory leak !!!