A constructor is a member function of a class which
initializes objects of a class. In C++, Constructor is
automatically called when object(instance of class)
create. It is special member function of the class.
 Constructor has same name as the class itself
 Constructors don’t have return type
 A constructor is automatically called when an object
is created.
 If we do not specify a constructor, C++ compiler
generates a default constructor for us (expects no
parameters and has an empty body).
 We can not refer to their addresses.
 An object with a constructor (or destructor) can not
be used as a member of a union.
 They make ‘implicit calls’ to the operators new and
delete when memory allocation is required.
 Default Constructor
 Parameterizes Constructor
 Copy Constructor
 Dynamic Constructor
 Default constructor is the constructor which doesn’t take any argument.
 It has no parameters.
 The default constructor for class A is A : : A ( )
Note: Even if we do not define any constructor explicitly, the compiler
will automatically provide a default constructor implicitly.
 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
they cannot return values.
 It may be necessary to initialize the various data
elements of different objects with different
values when they are created.
 This is achieved by passing arguments to the
constructor function when the objects are
created.
 The constructors that can take arguments are
called parameterized constructors.
 When an object is declared in a parameterized
constructor, the initial values have to be passed as
arguments to the constructor function.
 The normal way of object declaration may not work.
 The constructors can be called explicitly or
implicitly.
 It is used to initialize the various data elements of
different objects with different values when they are
created.
 It is used to overload constructors.
 In C++, We can have more than one constructor in
a class with same name, as long as each has a
different list of arguments. This concept is known
as Constructor Overloading and is quite similar
to Function Overloading.
 Overloaded constructors essentially have the same
name (name of the class) and different number of
arguments.
 A constructor is called depending upon the number
and type of arguments passed.
 While creating the object, arguments must be
passed to let compiler know, which constructor
needs to be called.
A copy constructor is a member function which
initializes an object using another object of the
same class. A copy constructor has the following
general function prototype:
ClassName (const ClassName &old_obj);
 A reference variable has been used as an
argument to the copy constructor.
 We cannot pass the argument by value to a copy
constructor.
continue …
A copy constructor is called when an object is passed by value.
Copy constructor itself is a function. So if we pass an argument
by value in a copy constructor, a call to copy constructor
would be made to call copy constructor which becomes a non-
terminating chain of calls. Therefore compiler doesn’t allow
parameters to be passed by value.
One reason for passing const reference is, we should use const
in C++ wherever possible so that objects are not accidentally
modified. This is one good reason for passing reference as
const
In C++, a Copy Constructor may be called in following cases:
 When an object of the class is returned by value.
 When an object of the class is passed (to a function) by value
as an argument.
 When an object is constructed based on another object of the
same class.
 When the compiler generates a temporary object.
When allocation of memory is done dynamically using
dynamic memory allocator new in a Constructor, it is known
as dynamic constructor. By using this, we can dynamically
initialize the objects.
 Allocation of memory to objects at the time of
their construction is known as dynamic
construction of objects.
 The memory is created with the help of the new
operator.
A destructor is used to destroy the objects that have been
created by a constructor.
OR
Destructor is a member function which destructs or deletes
an object.
A destructor function is called automatically when the object
goes out of scope:
(1) the function ends
(2) the program ends
(3) a block containing local variables ends
(4) a delete operator is called
 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.
 Destructors have same name as the class preceded by a tilde
(~)
 There cannot be more than one destructor in a class.
 They do not have any return type, just like constructors.
 When you do not specify any destructor in a class, compiler
generates a default destructor and inserts it into your code.
 Unlike constructors that can have parameters, destructors do
not allow any parameter.
~class_name()
{
//Some code
}
THANK YOU

ConsTRUCTION AND DESTRUCTION

  • 2.
    A constructor isa member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object(instance of class) create. It is special member function of the class.
  • 3.
     Constructor hassame name as the class itself  Constructors don’t have return type  A constructor is automatically called when an object is created.  If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body).
  • 4.
     We cannot refer to their addresses.  An object with a constructor (or destructor) can not be used as a member of a union.  They make ‘implicit calls’ to the operators new and delete when memory allocation is required.
  • 5.
     Default Constructor Parameterizes Constructor  Copy Constructor  Dynamic Constructor
  • 6.
     Default constructoris the constructor which doesn’t take any argument.  It has no parameters.  The default constructor for class A is A : : A ( ) Note: Even if we do not define any constructor explicitly, the compiler will automatically provide a default constructor implicitly.
  • 8.
     They shouldbe declared in the public section.  They are invoked automatically when the objects are created.  They do not have return types, not even void and they cannot return values.
  • 9.
     It maybe necessary to initialize the various data elements of different objects with different values when they are created.  This is achieved by passing arguments to the constructor function when the objects are created.  The constructors that can take arguments are called parameterized constructors.
  • 12.
     When anobject is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function.  The normal way of object declaration may not work.  The constructors can be called explicitly or implicitly.
  • 13.
     It isused to initialize the various data elements of different objects with different values when they are created.  It is used to overload constructors.
  • 14.
     In C++,We can have more than one constructor in a class with same name, as long as each has a different list of arguments. This concept is known as Constructor Overloading and is quite similar to Function Overloading.
  • 15.
     Overloaded constructorsessentially have the same name (name of the class) and different number of arguments.  A constructor is called depending upon the number and type of arguments passed.  While creating the object, arguments must be passed to let compiler know, which constructor needs to be called.
  • 18.
    A copy constructoris a member function which initializes an object using another object of the same class. A copy constructor has the following general function prototype: ClassName (const ClassName &old_obj);
  • 19.
     A referencevariable has been used as an argument to the copy constructor.  We cannot pass the argument by value to a copy constructor. continue …
  • 20.
    A copy constructoris called when an object is passed by value. Copy constructor itself is a function. So if we pass an argument by value in a copy constructor, a call to copy constructor would be made to call copy constructor which becomes a non- terminating chain of calls. Therefore compiler doesn’t allow parameters to be passed by value.
  • 21.
    One reason forpassing const reference is, we should use const in C++ wherever possible so that objects are not accidentally modified. This is one good reason for passing reference as const
  • 24.
    In C++, aCopy Constructor may be called in following cases:  When an object of the class is returned by value.  When an object of the class is passed (to a function) by value as an argument.  When an object is constructed based on another object of the same class.  When the compiler generates a temporary object.
  • 25.
    When allocation ofmemory is done dynamically using dynamic memory allocator new in a Constructor, it is known as dynamic constructor. By using this, we can dynamically initialize the objects.
  • 26.
     Allocation ofmemory to objects at the time of their construction is known as dynamic construction of objects.  The memory is created with the help of the new operator.
  • 28.
    A destructor isused to destroy the objects that have been created by a constructor. OR Destructor is a member function which destructs or deletes an object.
  • 29.
    A destructor functionis called automatically when the object goes out of scope: (1) the function ends (2) the program ends (3) a block containing local variables ends (4) a delete operator is called
  • 30.
     A destructornever 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.  Destructors have same name as the class preceded by a tilde (~)  There cannot be more than one destructor in a class.
  • 31.
     They donot have any return type, just like constructors.  When you do not specify any destructor in a class, compiler generates a default destructor and inserts it into your code.  Unlike constructors that can have parameters, destructors do not allow any parameter.
  • 32.
  • 35.