0% found this document useful (0 votes)
9 views37 pages

Chap Constructor Destructor

The document provides an overview of constructors and destructors in C++. It explains the purpose and characteristics of constructors, including default, parameterized, and copy constructors, as well as the role of destructors in cleaning up memory. Key points include how constructors initialize objects, the differences between constructors and regular member functions, and the importance of managing memory allocation and deallocation.

Uploaded by

Parteek 9D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views37 pages

Chap Constructor Destructor

The document provides an overview of constructors and destructors in C++. It explains the purpose and characteristics of constructors, including default, parameterized, and copy constructors, as well as the role of destructors in cleaning up memory. Key points include how constructors initialize objects, the differences between constructors and regular member functions, and the importance of managing memory allocation and deallocation.

Uploaded by

Parteek 9D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

Constructors and

Destructors
Constructors
• A Constructor is a member function of a class. It is
mainly used to initialize the objects of the class. It has
the same name as the class. When an object is created,
the constructor is automatically called. It is a special
kind of member function of a class.
• A constructor is a special member function whose task
is to initialize the objects of its class.
• It is special because its name is same as the class name.
• The constructor is invoked whenever an object of its
associated class is created.
• It is called constructor because it constructs the values
of data members of the class.
Difference Between Constructor and
Other Member Functions
1. The Constructor has the same name as the class
name.
2. The Constructor is called when an object of the
class is created.
3. A Constructor does not have a return type.
4. When a constructor is not specified, the compiler
generates a default constructor which does nothing.
Constructor - example
class add • When a class contains a
{ constructor, it is guaranteed
int m, n ; that an object created by the
public : class will be initialized
add (void) ; automatically.
------
}; • add a ;
add :: add (void) • Not only creates the object a of
{ type add but also initializes its
m = 0; n = 0; data members m and n to zero.
}
There are 3 types of constructors

• Default Constructor
• Parameterized Constructor
• Copy constructor
Constructors
continue …

• There is no need to write any statement to invoke


the constructor function.
• If a ‘normal’ member function is defined for zero
initialization, we would need to invoke this
function for each of the objects separately.
• A constructor that accepts no parameters is called
the default constructor.
• The default constructor for class A is A : : A ( )
Characteristics of Constructors
• They should be declared in the public section.

• They are invoked automatically when the objects


are created.

• They do not have return types, not even void and


they cannot return values.
Characteristics of Constructors
continue …

• They cannot be inherited, though a derived class


can call the base class constructor.

• Like other C++ functions, Constructors can have


default arguments.

• Constructors can not be virtual.


Characteristics of Constructors
continue …

• We can not refer to their addresses.

• An object with a constructor (or destructor) can


not be used as a member of a union.

• They make ‘implicit calls’ to the operators new


and delete when memory allocation is required.
Constructors
continue …

• When a constructor is declared for a class


initialization of the class objects becomes
mandatory.
Default Constructor

If no constructor is defined in the class then the


compiler automatically creates one for the program.
This constructor which is created by the compiler
when there is no user defined constructor and which
doesn’t take any parameters is called default
constructor.
#include <iostream>
using namespace std;
class test
{
public:
int y, z;
test()
{
y = 7;
z = 13;
}
};
int main()
{
test a;
cout <<"the sum is: "<<
a.y+a.z;
return 1;
Demo:: Demo()
{ X = 0; Y = 0;}

int main()
{
Demo d1 = Demo() ; //explicit call to default
constructor

d1.x =0
d1.y=0
Parameterized Constructors
• It may be necessary to initialize the various data
elements of different objects with different values
when they are created.

• This is achieved by passing arguments to the


constructor function when the objects are created.

• The constructors that can take arguments are


called parameterized constructors.
Parameterized Constructors
continue …

class add • When a constructor is


{ parameterized, we must pass
int m, n ; the initial values as arguments
public : to the constructor function
add (int, int) ; when an object is declared.
------
}; • Two ways Calling:
add : : add (int x, int y) o Explicit
• add sum = add(2,3);
{
m = x; n = y; o Implicit
• add sum(2,3)
}
• Shorthand method
The Parameterized Constructor
• Constructors should have any number of parameters,
• A Parameterized constructor is one that takes
arguments.
Multiple Constructors in a Class

• C + + permits to use more than one constructors


in a single class.

• Add( ) ; // No arguments

• Add (int, int) ; // Two arguments


Multiple Constructors in a Class
continue …

class add • The first constructor receives


{ no arguments.
int m, n ;
public :
add ( ) {m = 0 ; n = 0 ;} • The second constructor
add (int a, int b) receives two integer arguments.
{m = a ; n = b ;}
add (add & i) • The third constructor receives
{m = i.m ; n = i.n ;} one add object as an argument.
};
Multiple Constructors in a Class
continue …

class add • Add a1;


{ – Would automatically invoke the
int m, n ; first constructor and set both m
public : and n of a1 to zero.
add ( ) {m = 0 ; n = 0 ;} • Add a2(10,20);
add (int a, int b)
– Would call the second
{m = a ; n = b ;} constructor which will initialize
add (add & i) the data members m and n of a2
{m = i.m ; n = i.n ;} to 10 and 20 respectively.
};
Multiple Constructors in a Class
continue …

class add • Add a3(a2);


{ – Would invoke the third
int m, n ; constructor which copies the
public : values of a2 into a3.
add ( ) {m = 0 ; n = 0 ;} – This type of constructor is called
add (int a, int b) the “copy constructor”.
{m = a ; n = b ;} • Construction Overloading
add (add & i) – More than one constructor
{m = i.m ; n = i.n ;} function is defined in a class.
};
Multiple Constructors in a Class
continue …

class complex • complex ( ) { }


{
float x, y ;
public : – This contains the empty body and
complex ( ) { } does not do anything.
complex (float a)
{x=y=a;}
complex (float r, float i) – This is used to create objects
{x=r;y=i} without any initial values.
------
};
Multiple Constructors in a Class
continue …

• C + + compiler has an implicit constructor which


creates objects, even though it was not defined in
the class.
• This works well as long as we do not use any
other constructor in the class.
• However, once we define a constructor, we must
also define the “do-nothing” implicit constructor.
Constructors with Default Arguments

• It is possible to define constructors with default


arguments.
• Consider complex (float real, float imag = 0);
– The default value of the argument imag is zero.
– complex C1 (5.0) assigns the value 5.0 to the real
variable and 0.0 to imag.
– complex C2(2.0,3.0) assigns the value 2.0 to real and
3.0 to imag.
Constructors with Default Arguments
continue …

• A::A()  Default constructor


• A : : A (int = 0)  Default argument constructor

• The default argument constructor can be called with


either one argument or no arguments.
• When called with no arguments, it becomes a
default constructor.
Dynamic Initialization of Objects

• Providing initial value to objects at run time.

• Advantage – We can provide various initialization


formats, using overloaded constructors.

This provides the flexibility of using


different format of data at run time
depending upon the situation.
What is a copy constructor?

• A copy constructor is a member function which


initializes an object using another object of the
same class. A copy constructor has the following
general function prototype:

ClassName (const ClassName


&old_obj);
Copy Constructor

•A copy constructor is used to declare and initialize


an object from another object.

integer (integer & i) ;


integer I 2 ( I 1 ) ; or integer I 2 = I 1 ;
The process of initializing through a copy constructor
is known as copy initialization.
Copy Constructor
continue …

The statement
I 2 = I 1;
will not invoke the copy constructor.

If I 1 and I 2 are objects, this statement is legal and


assigns the values of I 1 to I 2, member-by-member.
Copy Constructor
continue …

• A reference variable has been used as an argument


to the copy constructor.

• We cannot pass the argument by value to a copy


constructor.
Copy Constructor
• Constructor that is used to initialize and declare an object
from another object is known as a copy constructor in
C++.

30
When is copy constructor called?
In C++, a Copy Constructor may be called in
following cases:
1. When an object of the class is returned by value.
2. When an object of the class is passed (to a
function) by value as an argument.
3. When an object is constructed based on another
object of the same class.
4. When the compiler generates a temporary object.
Dynamic Constructors

• The constructors can also be used to allocate


memory while creating objects.

• This will enable the system to allocate the right


amount of memory for each object when the
objects are not of the same size.
Dynamic Constructors
continue …

• Allocation of memory to objects at the time of their


construction is known as dynamic construction of
objects.

• The memory is created with the help of the “new”


operator.

• “New” is called memory management operator.


Destructors

• A destructor is used to destroy the objects that have


been created by a constructor.

• Like constructor, the destructor is a member


function whose name is the same as the class name
but is preceded by a tilde.
eg: ~ integer ( ) { }
Destructors
continue …

• A destructor never takes any argument nor does it


return any value.

• It will be invoked implicitly by the compiler upon


exit from the program – or block or function as the
case may be – to clean up storage that is no longer
accessible.
Destructors
continue …

• It is a good practice to declare destructors in a


program since it releases memory space for further
use.

• Whenever new is used to allocate memory in the


constructor, we should use delete to free that
memory.
Thank You

You might also like