SlideShare a Scribd company logo
Constructors & Destructors
Prof. K. Adisesha
Learning Outcomes
 Introduction
 Defining Constructors
 Characteristics of Constructor
 Types of constructor
 Constructor Overloading
 Destructors
 Characteristics of destructor
2
Introduction
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.
 Automatic de-allocating of memory when an object of
that class is destroyed is carried out using special member
functions called destructor.
3
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:
class MyClass // The class
{ public: // Access specifier
MyClass() // Constructor
{ cout << "Hello World!";
}
}; 4
Constructors
Characteristics of Constructor:
 The name of the constructor is the same as the name of the
class.
 A Constructor, even though it is a function, has no return type
function nor a void function.
 The constructor should be declared in the public section.
 Constructors are executed automatically.
 They are executed when a class object is created.
 A class can have more than one constructor.
 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.
5
Constructors
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.
6
Constructors
Types of constructor:
 Constructors are normally classified as follows:
 Default Constructors.
 Parameterized Constructors.
 Copy Constructors.
7
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:
 A default constructor function initializes the data member with no
argument.
 It can be explicitly written in the public section of the class.
 In case, default constructor is not defined in a program, the C++
compiler automatically generates it in a program.
 The purpose of the default constructor is to construct a default object of
the class type.
8
Constructors
Default Constructors:
 The general format of default constructor is as follows.
9
Syntax:
class Class_Name
{
public:
Class_Name( )
{
…….
}
};
Example:
class Number
{
public:
Number( )
{
n = 0;
}
};
Constructors
Disadvantages of default constructors are:
 When many objects of the same class are created, all
objects are initialized to same set of values by default
constructors.
 It is not possible to initialize different objects with
different initial values using default constructors.
10
Constructors
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.
 Some of the features of the parameterized constructors are:
 The parameterized constructors can be overloaded.
 For an object created with one argument, constructor with only one
argument is invoked and executed.
 The parameterized constructor can have default arguments and default
values.
11
Constructors
Parameterized Constructors:
 The general format of Parameterized constructor is as follows.
12
Syntax:
class Class_Name
{
public:
Class_Name(arg1, arg2)
{
…….
}
};
Example:
class Number
{
public:
Number(int a, int b )
{ if (a > b)
big = a;
else
big = b; }
};
Constructors
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:
 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.
13
Constructors
Copy Constructors:
 The general format of copy constructor is as follows.
14
Syntax:
class Class_Name
{
public:
Class_Name( Class_Name &ptr )
{
…….
}
};
Example:
class Number
{
public:
Number(int n)
{ a = n; }
Number(Number &X)
{ a = X.a;
cout<<”Copy Constructor
invoked”;
} };
Constructors
Invoking Constructors
 A Constructor is automatically invoked by C++ compiler
with an object declaration.
 The constructor can be invoked through the following
methods.
 Implicit Call
 Explicit Call
 Initialization with “ = ” Assignment operator
15
Invoking Constructors
Implicit Call:
 An Implicit call means the declaration of the object is followed
by argument list enclosed in parenthesis.
 Syntax:
Class_name Object(Arg1, Arg2,…)
 Example:
num obj1(10, 20); //Implicit Call
num obj2(40, 50); //Implicit Call
16
Invoking Constructors
Explicit Call:
 In explicit call, declaration of an object is followed by
assignment operator, constructor name and argument list
enclosed in parenthesis.
 Syntax:
Class_name Object=Constructor_name(Arg1, Arg2,…)
 Example:
num obj1 = num(10, 20); //Explicit Call
num obj2 = num(40, 50); //Explicit Call
17
Invoking Constructors
Initialization 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..
 Syntax:
Class_name Object=Arg1
 Example:
num obj1 = 100
num obj2 = 200
18
Invoking Constructors
#include<iostream.h>
#include<conio.h>
class num
{
private:
int a, b;
public:
num ( int m) // Constructor
{ a = m; b=m; }
num (int m, int n) // Constructor
{
a = m;
b = n;
}
void display( )
{
cout<<” a = “ << a <<” b = “ << b;
}
};
void main( )
{
num obj1(10, 20); //Implicit Call
num obj2=num(30, 40); //Explicit Call
Num obj3=50; //Assignment operator
obj1.display( );
obj2.display( );
Obj3.display( );
}
19
Program to initialize objects using Invoking Constructors
Constructor
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: class num
{
public:
num ( ); // Constructor with no agruments
num ( int m); // Constructor with one agrument
num (int m, int n); // Constructor with two agruments
}; 20
Destructors
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 (~).
21
Destructors
Destructors:
 The general format of destructor is as follows.
22
Syntax:
class Class_Name
{
public:
Class_Name( );
~ Class_Name( );
};
Example:
class Counter
{
public:
Counter( ) //Constructor
{
n = 0;
}
~Counter ( ) //Destructor
{ }
};
Destructors
Characteristics of destructor:
 The destructor name must have the same name as the
class preceded by a tilde (~).
 The destructor cannot take arguments therefore cannot be
overloaded.
 The destructor has no return type.
 There can be only one destructor in each class.
 In should have public access in the class declaration.
 The destructor cannot be inherited.
23
Discussion
Questions:
 What is Constructor? Give the rules for writing a constructor
function.
 What is default constructor? Write a program to illustrate it.
 Explain parameterized constructor with syntax and example.
 Mention the different methods by which constructor are
invoked. Explain anyone with an illustrative example.
 Explain the features of copy constructor.
 Explain destructor with syntax and example.
24

More Related Content

Constructors and destructors

  • 2. Learning Outcomes  Introduction  Defining Constructors  Characteristics of Constructor  Types of constructor  Constructor Overloading  Destructors  Characteristics of destructor 2
  • 3. Introduction 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.  Automatic de-allocating of memory when an object of that class is destroyed is carried out using special member functions called destructor. 3
  • 4. 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: class MyClass // The class { public: // Access specifier MyClass() // Constructor { cout << "Hello World!"; } }; 4
  • 5. Constructors Characteristics of Constructor:  The name of the constructor is the same as the name of the class.  A Constructor, even though it is a function, has no return type function nor a void function.  The constructor should be declared in the public section.  Constructors are executed automatically.  They are executed when a class object is created.  A class can have more than one constructor.  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. 5
  • 6. Constructors 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. 6
  • 7. Constructors Types of constructor:  Constructors are normally classified as follows:  Default Constructors.  Parameterized Constructors.  Copy Constructors. 7
  • 8. 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:  A default constructor function initializes the data member with no argument.  It can be explicitly written in the public section of the class.  In case, default constructor is not defined in a program, the C++ compiler automatically generates it in a program.  The purpose of the default constructor is to construct a default object of the class type. 8
  • 9. Constructors Default Constructors:  The general format of default constructor is as follows. 9 Syntax: class Class_Name { public: Class_Name( ) { ……. } }; Example: class Number { public: Number( ) { n = 0; } };
  • 10. Constructors Disadvantages of default constructors are:  When many objects of the same class are created, all objects are initialized to same set of values by default constructors.  It is not possible to initialize different objects with different initial values using default constructors. 10
  • 11. Constructors 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.  Some of the features of the parameterized constructors are:  The parameterized constructors can be overloaded.  For an object created with one argument, constructor with only one argument is invoked and executed.  The parameterized constructor can have default arguments and default values. 11
  • 12. Constructors Parameterized Constructors:  The general format of Parameterized constructor is as follows. 12 Syntax: class Class_Name { public: Class_Name(arg1, arg2) { ……. } }; Example: class Number { public: Number(int a, int b ) { if (a > b) big = a; else big = b; } };
  • 13. Constructors 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:  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. 13
  • 14. Constructors Copy Constructors:  The general format of copy constructor is as follows. 14 Syntax: class Class_Name { public: Class_Name( Class_Name &ptr ) { ……. } }; Example: class Number { public: Number(int n) { a = n; } Number(Number &X) { a = X.a; cout<<”Copy Constructor invoked”; } };
  • 15. Constructors Invoking Constructors  A Constructor is automatically invoked by C++ compiler with an object declaration.  The constructor can be invoked through the following methods.  Implicit Call  Explicit Call  Initialization with “ = ” Assignment operator 15
  • 16. Invoking Constructors Implicit Call:  An Implicit call means the declaration of the object is followed by argument list enclosed in parenthesis.  Syntax: Class_name Object(Arg1, Arg2,…)  Example: num obj1(10, 20); //Implicit Call num obj2(40, 50); //Implicit Call 16
  • 17. Invoking Constructors Explicit Call:  In explicit call, declaration of an object is followed by assignment operator, constructor name and argument list enclosed in parenthesis.  Syntax: Class_name Object=Constructor_name(Arg1, Arg2,…)  Example: num obj1 = num(10, 20); //Explicit Call num obj2 = num(40, 50); //Explicit Call 17
  • 18. Invoking Constructors Initialization 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..  Syntax: Class_name Object=Arg1  Example: num obj1 = 100 num obj2 = 200 18
  • 19. Invoking Constructors #include<iostream.h> #include<conio.h> class num { private: int a, b; public: num ( int m) // Constructor { a = m; b=m; } num (int m, int n) // Constructor { a = m; b = n; } void display( ) { cout<<” a = “ << a <<” b = “ << b; } }; void main( ) { num obj1(10, 20); //Implicit Call num obj2=num(30, 40); //Explicit Call Num obj3=50; //Assignment operator obj1.display( ); obj2.display( ); Obj3.display( ); } 19 Program to initialize objects using Invoking Constructors
  • 20. Constructor 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: class num { public: num ( ); // Constructor with no agruments num ( int m); // Constructor with one agrument num (int m, int n); // Constructor with two agruments }; 20
  • 21. Destructors 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 (~). 21
  • 22. Destructors Destructors:  The general format of destructor is as follows. 22 Syntax: class Class_Name { public: Class_Name( ); ~ Class_Name( ); }; Example: class Counter { public: Counter( ) //Constructor { n = 0; } ~Counter ( ) //Destructor { } };
  • 23. Destructors Characteristics of destructor:  The destructor name must have the same name as the class preceded by a tilde (~).  The destructor cannot take arguments therefore cannot be overloaded.  The destructor has no return type.  There can be only one destructor in each class.  In should have public access in the class declaration.  The destructor cannot be inherited. 23
  • 24. Discussion Questions:  What is Constructor? Give the rules for writing a constructor function.  What is default constructor? Write a program to illustrate it.  Explain parameterized constructor with syntax and example.  Mention the different methods by which constructor are invoked. Explain anyone with an illustrative example.  Explain the features of copy constructor.  Explain destructor with syntax and example. 24