Professional Documents
Culture Documents
&
DESTRUCTOR
PDPU
Introduction …
We write
int i = 0; // valid statement
but
We cannot initialize variables directly when we
define any class.
Why?
The memory allocation would be done only when
we create objects from this class.
2
Introduction …
Try following code & see the error.
class circle
{
int r = 0;
Public:
float area();
};
// Error: cannot initialize class member here.
WHY: No memory is allocated when you define a
class. 3
Introduction …
We have to pass value via function to initialize
variable of a class.
4
LOOK AT setradius()…
class circle
{
private:
int radius;
float area;
public:
void setradius(int r)
{
radius = r ;
};
float calc_area(void);
}; 5
Constructors …
C++ provides a special member function called
constructor that enables an object to initialize
itself when it is created.
Also known as automatic initialization of objects.
6
Constructors… example …
class circle public:
{ circle()
private: {
int radius; radius = 0;
float area;
};
float calc_area (float);
};
8
Defining constructors …
<class-name> :: <class-name>()
{
…
};
9
Few Points Before We Move Further …
10
Few Points Before We Move Further …
12
DECLARING PARAMETERIZED CONSTRUCTORS …
class <class-name>
{
…
public:
// constructor declared.
<class-name>(arguments);
…
};
13
Parameterized Constructors… Example …
14
The Object Declaration Statement…
16
Multiple Constructors… Example…
class circle
{
int radius;
public:
circle() { radius = 0 };
circle (int r) { radius = r };
circle (circle & c) { radius = c.radius };
…
};
17
Multiple Constructors… Example…
void main() {
//Constructor with no argument.
circle c1;
//Constructor with 1 argument.
circle c2 = circle(10);
circle c3(20); // Another way.
circle c4(c3); // Copy constructor.
};
See program constr2.cpp
18
Copy Constructor…
It is used to declare (create) and initialize an
object from another object.
e.g. Circle c4(c3);
Here c3 is object of class circle and values of all
variables would be copied to object c4.
19
Difference between Assignment Operator and
Copy Constructor…
20
Copy Constructor…
Copy constructor takes a reference to an object of
the same class as itself as an argument.
Click here: how the copy constructor is defined
When no copy constructor is defined, the
compiler supplies its own copy constructor.
23
new & delete Operators…
Object created by new will remain in existence
until it is destroyed by using delete.
Thus lifetime of an object is directly under our
control irrespective to the block structure of the
program.
new can be used to create objects of any type.
24
new & delete Operators…
SYNTAX:
pointer-variable = new data-type.
EXAMPLE:
int * p = new int;
float * f = new float;
*p = 30;
*f = 25.0;
will assign 30 and 25.0 to the newly created int and
float object respectively.
25
new & delete Operators…
27
new & delete Operators…
29
Destructors …
Will be invoked implicitly by the compiler upon
exit from:
Function (if class is defined within any
function.)
Or from the program.
Whenever new is used to allocate memory in the
constructors, use delete to free that memory.
31
Summary …
When an object is created and initialized at the
same time, a copy constructor gets called.
Use of new and delete operator allows us to
allocate and de-allocate memory for the object
dynamically.
Destructor is used to destroy the objects when
they are no longer required.
32