Chapter 9 Constructors
Chapter 9 Constructors
Chapter-9
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 member functions.
Automatic initialization is carried out using special member functions called constructors.
Constructors:
A Constructor is a special member function that is called automatically when an object is
created.
The purpose of a constructor is to mainly initialize the member variables of a class.
The general syntax of a the constructor in C++ is:
In the above example class declaration, class Sum has a member function Sum( ) with the same
name of the class and which provides initial value to its data member s.
A class can have more than one constructor. However all constructor of a class should have the
same name.
It is not possible to refer to the address of the constructors.
The constructors make implicit calls to the operator new and delete when memory allocation is
required.
Example: Program to demonstrate how constructor is automatically executed at the time of
object creation.
#include<iostream.h>
#include<conio.h>
class Student
{
public:
Student( )
{
cout<<”Constructor called automatically”;
cout<<”at the time of object creation”<<endl;
}
};
void main( ) OUTPUT:
{ Constructor called automatically at the time of object creation
Student S1;
Constructor called automatically at the time of object creation
Student S2;
Student S3; Constructor called automatically at the time of object creation
}
Example: Program to demonstrate how a constructor is use to initialize data member of an
object.
#include<iostream.h>
#include<conio.h>
class Number
{
private:
int a;
public:
Number ( )
{
cout<<”I am in the Constructor”;
a = 100;
}
void display( )
{
cout<<”Value of a is =”<<a;
}
2|Page
Chapter 9- Constructors and Destructors
};
void main( ) OUTPUT:
{ I am in the Constructor
Number N;
Value of a is = 100
N.display; Importan
}
t2
Marks
Need for a Constructor:
Constructors are named as constructors because they are getting called when an object is
constructed.
The use of a constructor can be cleverly done especially in those problems where it is necessary to
initialize certain data members compulsorily.
Instead of having separate member functions for initializing we can perform those operations
inside the constructor itself.
Example: A Program to find the sum of N natural numbers using a class constructor.
#include<iostream.h>
#include<conio.h>
class Sum
{
private:
int n, s;
public:
Sum ( )
{
s = 0;
}
void readdata( )
{
cout<<”Enter the input limit”<<endl;
cin>>n;
}
void display( )
{
for(int i =1; i<=n; i++)
s = s + i;
cout<<”Sum of Natural numbers =”<<s;
}
};
void main( ) OUTPUT:
{ Enter the input limit
Sum S1;
S1.readdata( ); 10
S1.display( ); Sum of Natural numbers = 55
3|Page
Chapter 9- Constructors and Destructors
}
4|Page
Chapter 9- Constructors and Destructors
Types of constructor:
Constructors are normally classified as follows:
o Default Constructors. Important
o Parameterized Constructors 5 Marks
o Copy Constructors.
Default Constructors:
A default constructor is a special member function which is invoked by the C++ compiler
without any argument for initializing the object of a class.
It is also called as zero argument constructors.
Some of the features of the default constructors are:
o A default constructor function initializes the data member with no argument.
o It can be explicitly written in the public section of the class.
o In case, default constructor is not defined in a program, the C++ compiler automatically
generates it in a program.
o The purpose of the default constructor is to construct a default object of the class type.
The general format of default constructor is as follows:
Syntax Example
class Class_Name class Number
{ {
public: public:
Class_Name( ) Number( )
{ {
……. n = 0;
} }
}; };
Example: A program to display N natural numbers using a class default constructor.
#include<iostream.h>
#include<conio.h>
class Number
{
private:
int n;
public:
Number ( ) //Default Constructor with no arguments
{
n = 0;
}
void readdata( )
{
5|Page
Chapter 9- Constructors and Destructors
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 objects.
The keyword inline is used to define inline function.
The general format of parameterized constructor is as follows:
Syntax Example
class Class_Name class MAX
{ {
public: public:
Class_Name( argu1, argu2….) MAX(int a, int b )
{ {
……. if (a > b)
} big = a;
}; else
big = b;
}
};
6|Page
Chapter 9- Constructors and Destructors
Invoking Constructors:
A Constructor is automatically invoked by C++ compiler with an object declaration. The
constructor can be invoked through the following methods.
o Implicit Call
o Explicit Call
Important
o Initialization at the time of declaration with “ = “ operator. 5 Marks
Implicit Call:
An Implicit call means the declaration of the object is followed by argument list enclosed in
parenthesis.
Example: Program to initialize the data members using implicit declaration
#include<iostream.h>
#include<conio.h>
class num
{
private:
int a, b;
public:
num ( int m, int n) //Parameterized Constructor
{
a = m;
b = n;
}
void display( )
{
cout<<” a = “ << a <<” b = “ << b;
}
};
void main( )
{ OUTPUT:
num obj1(10, 20); //Implicit Call a = 10 b = 20
num obj2(40, 50); //Implicit Call
obj1.display( ); a = 40 b = 50
obj2.display( );
}
7|Page
Chapter 9- Constructors and Destructors
Explicit Call:
In explicit call, declaration of an object is followed by assignment operator, constructor name and
argument list enclosed in parenthesis.
Example: Program to initialize the data members using explicit declaration
#include<iostream.h>
#include<conio.h>
class num
{
private:
int a, b;
public:
num ( int m, int n) //Parameterized Constructor
{
a = m;
b = n;
}
void display( )
{
cout<<” a = “ << a <<” b = “ << b;
}
};
void main( )
{
num obj1 = num(10, 20); //Explicit Call OUTPUT:
num obj2 = num(40, 50); //Explicit Call
a = 10 b = 20
obj1.display( );
obj2.display( ); a = 40 b = 50
}
}
void display( )
{
cout<< a << endl ;
}
};
void main( )
{
num obj1 = 100
num obj2 = 200
cout<<”Object1 = “ ; OUTPUT:
obj1.display( );
Object1 = 100
cout<<”Object2 = “ ;
obj2.display( ); Object2 = 200
}
Copy Constructors:
Copy constructor is a parameterized constructor using one object can be copied to another
object.
Copy Constructors are used in the following situations:
o To initialize an object with the values of already existing objects.
o When objects must be returned as function values.
o 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.
The general format of copy constructor is as follows:
Syntax Example
class Class_Name class Number
{ {
public: public:
Number(int n)
Class_Name( Class_Name &ptr )
{ a = n; }
{ Number(Number & X)
……. {
} a = X.a;
}; cout<<”Copy Constructor invoked”;
}
};
Note that:
o Copy constructor is not invoked explicitly.
o Copy constructors are invoked automatically when a new object is created and equated to
an already existing object in the declaration statement itself.
9|Page
Chapter 9- Constructors and Destructors
10 | P a g e
Chapter 9- Constructors and Destructors
Constructor Overloading:
A class has two or more constructor functions with the same name but different signatures are
called Constructors Overloading.
Depending upon the type of argument, the constructors will be invoked automatically by the
compiler to initialize the objects.
Example: Program to find simple interest using constructor overloading.
#include<iostream.h>
#include<conio.h>
class simpleinterset
{
private:
float p, r, t, si;
public:
simpleinterset( ) //Default constructor
{
}
simpleinterset(float x, float y, float z) //Parameterized Constructor
{
p = x;
r = y;
t = z;
}
void compute ( )
{
si = (p * t * r)/100;
cout<<”Simple Interest is = “<< si;
}
};
void main( )
{
OUTPUT:
simpleinterest S1, S2(10000.0, 12.0, 2.0);
S2.compute( ); Simple Interest is = 2400
}
Destructors:
A destructor is special member function that is executed when an object of that class is
destroyed.
Destroying an object means, de-allocating all the resources such as memory that was allocated for
the object by the constructor.
It will have like constructor, the name same as that of the class but preceded by a tilde (~).
The general format of destructor is as follows:
11 | P a g e
Chapter 9- Constructors and Destructors
Syntax Example
class Class_Name class Counter
{ {
public: public:
Class_Name( ); Counter( ) //Constructor
~ Class_Name( ); {
}; n = 0;
}
~Counter ( ) //Destructor
{ }
};
12 | P a g e
Chapter 9- Constructors and Destructors
} OUTPUT:
void main( )
{ In Constructor:
num a; Value of X = 100
a.display( ); In Destructor
}
Important Questions
5 Marks Question:
1. What is Constructor? Give the rules for writing a constructor function.
2. What is default constructor? Write a program to illustrate it.
3. Explain parameterized constructor with syntax and example.
4. Mention the different methods by which constructor are invoked. Explain anyone
with an illustrative example.
5. Explain the features of copy constructor.
6. Explain destructor with syntax and example.
7. Write a C++ program to find the sum of the series 1+X+X2+….+Xn using
constructor:
#include<iostream.h>
#include<conio.h>
class copy
{
private:
int x, n;
public:
int compute;
copy(int xx, int nn)
{
x = xx;
n = nn;
}
};
13 | P a g e
Chapter 9- Constructors and Destructors
*************
14 | P a g e