0% found this document useful (0 votes)
118 views

4-Constructors and Destructors

Constructors are special member functions that initialize objects. There are three main types of constructors: 1) Default constructor - Initializes all objects to the same values with no arguments. 2) Parameterized constructor - Initializes objects with different values by passing arguments. 3) Copy constructor - Initializes one object using the values of another existing object. Copy constructors are invoked automatically during object copying or passing by value.

Uploaded by

Chandana M
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)
118 views

4-Constructors and Destructors

Constructors are special member functions that initialize objects. There are three main types of constructors: 1) Default constructor - Initializes all objects to the same values with no arguments. 2) Parameterized constructor - Initializes objects with different values by passing arguments. 3) Copy constructor - Initializes one object using the values of another existing object. Copy constructors are invoked automatically during object copying or passing by value.

Uploaded by

Chandana M
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/ 7

Constructors & Destructors St.Joseph’s Convent Girls P.

U College,
Chickballpur

Constructors & Destructors


1. What is Constructor?
Constructors are the special member function of the class which are used to initialize
the objects of that class automatically.

2. Write the rules to be followed in writing constructor in C++.


 A Constructor has same name that of the class name. This will help the
compiler to identify that they are the constructors.
 There is no return type, not even void.
 A constructor should be defined or declared using public access specifier.
 A constructor is invoked automatically when objects are created. It is not
possible to refer to the address of the constructors.
 The constructors make implicit calls to the operators new and delete when
memory allocation is required.

Declaration of constructor inside the class definition


Example: class example
{
int a;

public:
example () //constructor inside the class definition
{
a = 0;
}
};

3. Mention the different types of Constructors.


a. Default Constructor
b. Parameterized Constructor
c. Copy Constructor

Default Constructor
 A constructor which does not accept any arguments is called a zero argument
constructoror default constructor.
 Default constructor simply allocates memory to data members of objects.
Features of Default Constructor
 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 constructor.

Dept. Of Computer Science Page 1


Constructors & Destructors St.Joseph’s Convent Girls P.U College,
Chickballpur

Example:
Syntax: book()
classname::classname()//constructor without arguments {
{

}
}
Program to show initialization of an object using default constructor
#include<iostream.h>
#include<iomanip.h>
class decon
{
private:
int x, y;
public:
decon ( )
{
x=10; y=20;
}
void display()
{
cout<<"x = "<<x<<"y = "<<y<<endl;
}
}; x = 10 y = 20
x = 10 y = 20
void main()
{
decon D1,D2;
D1.display();
D2.display();
}
Disadvantages

 When many objects of the same class are created, all objects are initialized to
same set of values by default constructor.
 It is not possible to initialize different objects with different initial values.
Parameterized Constructors
 A constructor that takes one or more arguments is called parameterized 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 data members of the object.
Features:
 Parameterized constructors can be overloaded.
 For an object created with one argument, constructor with only one argument is invoked
and executed.

Dept. Of Computer Science Page 2


Constructors & Destructors St.Joseph’s Convent Girls P.U College,
Chickballpur

 Parameterized constructor can have default arguments.


Invoking constructors
 When an object is created, constructors will be invoked automatically.
 There are three ways to invoke a constructor.
 Explicit call
 Implicit call
 Initialization using assignment operator
Explicit callDeclaration of an object is followed by assignment operator followed
constructor function calling by passing arguments.
Example: num obj1=num(10,20);
num obj2=num(40,50);
To illustrate the use of 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 N1=num(10,20); //Explicit call of object1
num N2=num(40,50); //Explicit call of object2
N1.display();
N2.display();
}
Implicit call:An Implicit call means the declaration of the object is followed by argument list
enclosed in parentheses.
Example: num obj1(10,20);
num obj2(40,50);
To illustrate the use of parameterized constructor through implicit call.
#include<iostream.h>
class num

Dept. Of Computer Science Page 3


Constructors & Destructors St.Joseph’s Convent Girls P.U College,
Chickballpur

{
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 N1(10,20); //Implicit call of object1
num N2(40,50); //Implicit call of object1
N1.display();
N2.display();
}
Initialization using assignment operator or constructor with one parameter

 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.
 This method is applicable only to the constructors that have exactly one
parameter.
 Example: num obj1=100;
num obj2=400;
#include<iostream.h>
class num
{
private:
int a;
public:
num(int p)
{
a = p;
}
void display()
{
cout<<"a = "<<a<<endl;
}
};
void main()
{
num N1=100;
num N2=400;
N1.display();

Dept. Of Computer Science Page 4


Constructors & Destructors St.Joseph’s Convent Girls P.U College,
Chickballpur

N2.display(); }
Copy constructor It is a parameterized constructor.
One object can be copied to another object.
Copy constructors are used when:
 To initialize an object with the values of existing object.
 To return an object as return-type.
 To state objects as pass-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.
Note: 1.Copy constructor is not invoked explicitly.
2. Copy constructor is invoked automatically when a new object is created and
equated to an already existing object in the declaration statement itself.
Example: x a1; //default constructor
x a2 = a1; //copy constructor
a1.display ();
Copy constructor creates a new object a2 using existing object a1.
3. When a new object is declared and existing object is passed as a parameterto it,
then also copy constructor is invoked.
Example: x a1(100,200); //parameterized constructor
x a2(a1); //copy constructor is invoked for object a2
with a1 as parameter
4. When an object is passed to a function using pass-by-value, copy constructor is
automatically called.
Example: void test(x b)
{
—————
—————
}
void main()
{
x a;
a.test(a); //copy constructor is invoked
}
5. Copy constructor is invoked when an object returns a value.

Constructor Overloading
 Constructors are used to initialize the data members of a class.

Dept. Of Computer Science Page 5


Constructors & Destructors St.Joseph’s Convent Girls P.U College,
Chickballpur

 Overloading a constructor means specifying additional operation by passing the


required arguments.
 We can define any number of overloaded constructors in a single class.
 Depending on the type and number of arguments, the compiler decides invoke of
particular constructor.
simpleinterest(){} //Default constructor
simpleinterest(float x, float y) //Parameterized constructor
{
p = x;
r = y;
t = 5.5;
}

simpleinterest(float x, float y, float z) //Parameterized constructor


{
p = x;
r = y;
t = z;
}

Explain destructor with syntax and example.

Destructors are special member functions of the class required to free the memory of
the object whenever it goes out of scope.

 A Destructor has same name that of the class name preceded by a tilde symbol (~).
 Destructor is a special member function that de-allocates the memory allocated to the
object.
 A destructor will be called automatically when an object to be destroyed.
 Destroying an object means, de-allocating all the resources such as memory that was
allocated for the object by the constructor.
 Destructors do not have a return value. Destructor can never return a value.
 They take no arguments. Therefore, destructors cannot be overloaded.

Syntax: classclassname
{
private:
//datavariables
public:
classname(); //constructor
~classname(); //destructor
};

Dept. Of Computer Science Page 6


Constructors & Destructors St.Joseph’s Convent Girls P.U College,
Chickballpur

Program to show the use of destructor

#include<iostream.h>
#include<conio.h>
class number
{
private:
int a;
public:
number(); //Constructor
void display();
~number(); //Destructor
};

number::number()
{
cout<<“Constructor: ”<<endl;
a=10;
}
number::~number()
{
cout<<“Destructor ”<<endl;
}
void number::display()
{
cout<<“Value of a = ”<<a<<endl;
}
int main()
{
number N;
N.display();
return 0;
}

Dept. Of Computer Science Page 7

You might also like