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

Constructors and Destructors

The document discusses constructors and destructors in C++, explaining that constructors are special member functions used to initialize objects automatically, while destructors are called when an object is destroyed to deallocate resources. It details the types of constructors, including default, parameterized, and copy constructors, along with their features and usage. The document also outlines the rules for defining constructors and the syntax for destructors.

Uploaded by

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

Constructors and Destructors

The document discusses constructors and destructors in C++, explaining that constructors are special member functions used to initialize objects automatically, while destructors are called when an object is destroyed to deallocate resources. It details the types of constructors, including default, parameterized, and copy constructors, along with their features and usage. The document also outlines the rules for defining constructors and the syntax for destructors.

Uploaded by

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

𝑪𝑯𝑨𝑷𝑻𝑬𝑹 − 𝟗

CONSTRUCTORS AND DESTRUCTORS

Introduction:
It is sometimes convenient if an object can initialize itself when it is first created, without the
need to make a separate call to a member function. This is possible with special member
functions. Automatic initialization is carried out using such a special member function called
constructor. Generally, we can say, a constructor constructs the data members of an object.
i.e., initialized as a value to the data members.
“A constructor is a special member function that is used in classes to initialize the objects of a
class automatically”.

A constructor is a member function of a class with the same name as that of the class. A
constructor is defined like other member functions of a class. It can be defined either inside
the class definition or outside the class definition.

It is called constructor because it constructs the values of data members of the class.

The following rules are used for writing a constructor function:

1. A Constructor always has name that is same as the class name of which they are the
members. This will help the compiler to identify that they are the constructors.

2. There is no return type for constructors (not even void). Since, the constructor is called
automatically by the system, there is no program for it to return anything to; a return
value would not make sense.

3. A constructor should be declared in public section.

4. A constructor is invoked automatically when objects are created. Constructors can have
default arguments.

5. It is not possible to refer to the address of the constructors.

6. The constructors make implicit calls to the operators new and delete when
memory allocation is required.
Declaration of constructor inside the class definition
Example 9.1: class test

int m, x, y; public:

test() //constructor inside the class definition

{
x = 0; y = 0;

Note that when a constructor is declared for a class, initialization of class objects is done
automatically.

Types of constructors:
There are three types of constructors, namely:

Default constructor

Parameterized constructor

Copy constructor

Default Constructor
A constructor which does not accept any arguments is called a zero argument constructor. It is
also known as default constructor. The default constructors are useful when the objects are
need to be created without having to type initial values. Default constructor simply allocated
memory to data members of objects.

Features of default constructor are

• For every object created, this constructor is automatically called.


• All objects of a class are initialized to same set of values by the default constructor.
If different objects are to be initialized with different values, it cannot be done using default
Syntax:
classname::classname //constructor without arguments
{}

Example : student() { }

Program : To show initialization of an object using default constructor. #include<iostream.h>


#include<iomanip.h>
class x
{
private:
int a, b; public:
x() { a=10, b=20; }
void display()
{
cout<<a<<setw(5)<<b<<endl;
}
};
void main()
{
X obj1,obj2;
obj1.display(); obj2.display();
}
Disadvantages of default constructor:
⮚ When many objects of the same class are created, all objects are initialized to same
set of values by default constructor
Parameterized Constructors:
A constructor that takes one or more arguments is called parameterized constructor. Using
this constructor, it is possible to initialize different objects with different values.

Parameterized constructors are also invoked automatically, whenever objects with arguments
are created. The parameters are used to initialize the object.

Features of parameterized constructors:


• The parameterized constructors can be overloaded.
• For an object created with one argument, constructor with only one argument is invoked
and executed.
Invoking constructors

A constructor is automatically invoked by C++ compiler with an object declaration. The


constructors can be invoked through the following methods:

1. Explicit call
2. Implicit call
3. Initialization at the time of declaration with = operator
Explicit call
In explicit call, declaration of an object is followed by assignment operator, constructor name
and argument list enclosed in parenthesis.

Program 9.: To use parameterized constructor through explicit call

#include<iostream.h>

class num

private:

int a,b; public:

num(int p, int q)

a = p,b = q;

void display()

{
cout<<"a = "<<a<<" and b = "<<b<<endl;

};

void main()

num obj1=num(10,20); num obj2=num(40,50);

cout<<"First construction: ";

obj1.display();

cout<<"Second construction: ";

obj2.display();

In the above program code the objects obj1 and obj2 are called explicitly by using
parameterized constructor.

Implicit call
An Implicit call means the declaration of the object is followed by argument list enclosed in
parentheses.

Program : Program to intialise the data members using implicit declaration


#include<iostream.h>

class num

private:

int a,b; public:

num(int m, int n)

{ a = m, b = n; } void display()

cout<<"a= "<<a<<" b= "<<b<<endl;

};

void main()

num obj1; num obj1(10,20);


num obj2(40,50); obj1.display(); obj2.display();

Initialization of objects during declaration with assignment operator


This method is used for the constructor with exactly one argument. In this method declaration
is followed by assignment operator and value to be initialized.
Program : To initialize objects using assignment operator #include<iostream.h>
class num
{
private:
int a; public:
num(int m) { a = m; } void display()
{
cout<<a<<endl;
}
};
void main()
{
num obj1=100; num obj2=200;
cout<<"Object1 = ";obj1.display(); cout<<"Object2 = ";obj2.display();
}
In the above example, the constructors obj1 and obj2 are initialized using
= operator.
Copy constructor
Copy constructor is a parameterized constructor using which one object can be copied to
another object. Copy constructors are used in the following situations.
• To initialize an object with the values of already existing objects.
• When objects must be returned as function values
• To state objects as by value parameters of a function.
Copy constructor can accept a single argument of reference to same class type. The argument
must be passed as a constant reference type.
Syntax: classname :: classname(classname &ptr) Example : x::x(x &ptr)
Here, x is a class name and ptr is a pointer to a class object x.
If there is more than one argument present in the copy constructor, it must contain default
arguments.
Example : void test( x )
{
—————
—————
}
main()
{
x a;
test(a); //copy constructor is invoked
}
Constructor Overloading
Constructors are used to initialize the data members of a class. We can use constructors also to
specify the input values for the data members. But the default constructor cannot be used for
this purpose. So, we have to overload the default constructor. Overloading a constructor
means specifying additional operation by passing the required arguments. Depending on the
requirement one can define any number of overloaded constructors in a single class.
Depending on the type and number of arguments passed, the compiler decides which version
of the constructor to invoke during object creation. The following example has overloaded
constructor that is used to specify the input values for the data members of the class.

Program : To find the simple interest


#include<iostream.h>
class simpleinterest
{
private:
float p, r, t, si; public:
simpleinterest() //default constructor
{}
simpleinterest(float x, float y, float z)//parameterized
{ //constructor
p = x; r = y; t = z;
}
void computesi()
{
cout<<"Simple interest is :"<< (p * r * t)/100.0;
}
};
void main()
{
simpleinterest si1, si2(10000.0, 12.0, 2.0); si2.computesi();
}

In the above code segment, there are two constructors, one is simpleinterest() and another
one is simpleinterest(float x, float y, float z). The first one is the default constructor and the
second one is the three-argument constructor. Destructors:
A destructor will be called automatically when an object is destroyed. Destroying an object
means, de-allocating all the resources such as memory that was allocated for the object by the
constructor. A destructor is a special member function that will be executed automatically
when an object is destroyed. It will have, like constructor, the name same as that of the class
but preceded by a tilde (~).

Syntax: class classname

{
private:

//data variables

//method public:

classname(); //constructor

~classname(); //destructor

• The destructor name is same as that of class. The first character must be tilde (~).
• Destructors do not have a return value. Destructor can never return a value.
• They take no arguments. Therefore destructors cannot be overloaded.
• The most common use of destructors is to de-allocate memory that was allocated for the
object by the constructor. Also, they are declared public.
.

∗∗∗∗∗

You might also like