0% found this document useful (0 votes)
10 views4 pages

Chapter 6

C++ provides constructors for automatic object initialization and destructors for object destruction when no longer needed. Constructors can be default or parameterized, support overloading, and cannot return values or be virtual. Destructors, named similarly to constructors but with a tilde, clean up memory and do not take arguments or return values.

Uploaded by

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

Chapter 6

C++ provides constructors for automatic object initialization and destructors for object destruction when no longer needed. Constructors can be default or parameterized, support overloading, and cannot return values or be virtual. Destructors, named similarly to constructors but with a tilde, clean up memory and do not take arguments or return values.

Uploaded by

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

6.

Constructors and
Destructors
C++ provides a special member function called the
constructor which enables an object to initialize itself when it
is created. This is known as automatic initialization of objects.
It also provides another member function called the
destructor that destroys the objects when they are no longer
required.

A constructor is a ‘special’ member function whose task is to


initialize the objects of its class. It is special because its name
is the 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.

A constructor that accepts no parameters is called the default


constructor. The default constructor for class A is A::A( ). If
no such constructor is defined, then the compiler supplies a
default constructor. Therefore a statement such as
A a;
invokes the default constructor of the compiler to create the
object a.

The constructor functions have some special characteristics.


These are :
• 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 therefore,
they cannot return values.
• They cannot be inherited, though a derived class can call
the base class constructor.
• Like other C++ functions, they can have default arguments.
• Constructors cannot be virtual.
• We cannot refer to their addresses.
• An object with a constructor (or destructor) cannot be used
as a member of a union.
• They make ‘implicit calls’ to the operators new and delete
when memory allocation is required.
It may be necessary to initialize the various data elements of
different objects with different values when they are created.
C++ permits us to achieve this objective by passing
arguments to the constructor function when the objects are
created. The constructors that can take arguments are called
parameterized constructors.

We must pass the initial values as arguments to the


constructor function when an object is declared. This can be
done in two ways:
integer int1 = integer(0,100); // explicit call
integer int1(0,100); // implicit call

The constructor functions can also be defined as inline


functions.

The parameters of a constructor can be of any type except


that of the class to which it belongs. However, a constructor
can accept a reference to its own class as a parameter. In
such cases, the constructor is called the copy constructor.

Multiple Constructors in a Class.

When more than one constructor function is defined in a


class, we say that the constructor is overloaded.

C++ compiler has an implicit constructor which creates


objects, even though it was not defined in the class. This
works fine as long as we do not use any other constructors in
the class. However, once we define a constructor, we must
also define the “do-nothing” implicit constructor. This
constructor will not do anything and is defined just to satisfy
the compiler.

It is possible to define constructors with default arguments.


The actual parameter, when specified, overrides the default
value. As pointed out earlier, the missing arguments must be
the trailing ones.

The default argument constructor can be called with either


one argument or no arguments. When called with no
arguments, it becomes a default constructor.
copy constructor format
integer(integer &i);//constructor prototype and declaration.
integer I2(I1);//constructor calling
Another form of this statement is
integer I2 = I1;
The process of initializing through a copy constructor is
known as copy initialization.

Remember, the statement


I2 = I1;
will not invoke the copy constructor. However, if I1and I2are
objects, this statement is legal and simply assigns the values
of I1to I2, member-by-member. This is the task of the
overloaded assignment operator(=).

We cannot pass the argument by value to a copy constructor.

When no copy constructor is defined, the compiler supplies


its own copy constructor.

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, thus resulting in the saving of memory.
Allocation of memory to objects at the time of their
construction is known as dynamic construction of objects.
A constant object can call only const member functions.
Whenever const objects try to invoke nonconst member
functions, the compiler generates errors.

A destructor, as the name implies, is used to destroy the


objects that have been created by a constructor. Like a
constructor, the destructor is a member function whose name
is the same as the class name but is preceded by a tilde.

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. It is a good
practice to declare destructors in a program since it releases
memory space for future use.

You might also like