Constructors & Destructors
Pranali Chaudhari
Contents
 Constructor
 Types of Constructor
 Memory Allocation
 Destructor
Constructor
 A constructor is a special member function
whose task is to initialize the objects of its class.
 Constructor has the same name as that of
class.
 The constructor is invoked when the object of its
associated class is created.
 Constructor constructs the values of data
members of the class.
Constructor
 A constructor is declared and defined as follows:
Class integer
{
int m, n;
public :
integer(void); // constructor declared
…..
…..
};
integer :: integer(void) // constructor defined
{
m = 0;
n = 0;
}
Characteristics of Constructor
 The constructor have some special characteristics:
 They should be declared in the public section.
 They are invoked automatically when the objects are
created.
 They do not have return type.
 They cannot be inherited.
 Like other C++ functions, they can have default
arguments.
 Constructors cannot be virtual.
 They make implicit calls to the operators new and delete
when memory allocation is required.
Types of Constructors
 Default constructor.
 Parameterized constructor.
 Copy constructor.
 Dynamic constructor.
Default Constructor
 A constructor that accepts no parameters is
called the default constructor.
 The default constructor for class A is A::A().
 If no such constructor is defined, then the
compiler supplies a default constructor.
Parameterized Constructor
 The constructor that can take arguments are called
parameterized constructors.
class integer
{
int m, n;
public:
integer(int x, int y); // parameterized constructor
…..
…..
};
integer :: integer(int x, int y)
{
m = x;
n = y;
}
Copy Constructor
 A copy constructor can take as argument an object of
the same class to which it belongs.
 A copy constructor is used to declare and initialize an
object from another object.
integer I2(I1);
 Define the object I2 and at the same time initialize it
to the values of I1.
integer I2 = I1;
Multiple constructor in a class
class integer
{
int m, n;
public:
integer(){ m = 0; n = 0; } // constructor 1
integer(int a, int b) // constructor 2
{ m = a; n = b; }
integer( integer & i ) // constructor 3
{ m = i.m; n = i.n; }
};
Dynamic Constructor
 The constructors can also be used to allocate
memory while creating objects.
 Allocation of memory to objects at the time of their
construction is known as dynamic construction of
objects.
 The memory is allocated with the help of the new
operator.
Dynamic Constructor
class string
{
char *name;
int length;
public:
string()
{
length = 0;
name = new char [ length +1];
}
string (char *s)
{
length = strlen(s);
name = new char [ length + 1];
strcpy(name, s);
}
void display(void)
{ cout << name << “n” ; }
void join(string &a, string &b);
};
Dynamic Constructor
void string :: join(string &a, string &b)
{
length = a.length + b.length;
delete name;
name = new char [ length + 1];
strcpy(name, a.name);
strcat(name, b.name);
}
int main()
{
char *first = “Mathew”;
string name1(first), name2(“Joseph”), s1;
s1.join(name1, name2);
s1.display();
return 0;
}
Constructor with Default
Arguments
 Constructors can be defined with default
arguments.
 Eg: complex(float real, float imag=0);
 complex c (5.0);
 complex c(2.0, 3.0);
Dynamic Initialization of Objects
 Class objects can be initialized dynamically.
 The initial value of object is provided during
runtime.
 Dynamic initialization can be provided using
overloaded constructors.
 Example.
Destructor
 A destructor is used to destroy the objects
that have been created by a constructor.
 It is defined as:
~ integer() { }
 A destructor never takes any arguments nor
does it return any value.
Practice Example
 Create a class student with the data members
roll no, student name and percentage marks.
Create a default constructor to initialize each
member as zero. Demonstrate all the types of
constructors to initialized the objects. Display
all the objects.
Summary
 ______ are used to initialize variable and to
allocate memory.
 Constructor has the same name as that of
class. ( True / False )
 A constructor that takes arguments is known
as __________ .
 A _________ is used to declare and initialize
an object from another object.
 The memory is allocated with the help of the
_____ operator.
 Objects are destroyed by using ________.
Short Answer Questions
 How do we invoke a constructor function?
 The constructor is invoked whenever an object
of its associated class is created.
 List some of the special properties of the
constructor.
 Properties of constructors
Short Answer Questions
 What do you mean by dynamic initialization of
objects? Why do we need this?
 Dynamic initialization of objects means
providing the values to an object at runtime.
This provides the flexibility of using different
format of data at run time depending upon the
situation.
Short Answer Questions
 Explain the following statements:
time T2 (T1);
time T2 = T1;
T1 and T2 are objects of time class.
 Both the statements are demonstrating the
copy constructor. Here the object T2 of class
time is created and at the same time it is
initialized with the values of another object T1
of class time.
Short Answer Questions
 Explain the term constructor overloading.
 Constructor overloading means when more than
one constructor is defined in a class.
 What is default constructor? What happens if no
such constructor is defined?
 A constructor that accepts no parameters is called
the default constructor. If no such constructor is
defined, then the compiler supplies a default
constructor.
References
 Object Oriented Programming with C++ by E.
Balagurusamy.
END OF UNIT

Constructors destructors

  • 1.
  • 2.
    Contents  Constructor  Typesof Constructor  Memory Allocation  Destructor
  • 3.
    Constructor  A constructoris a special member function whose task is to initialize the objects of its class.  Constructor has the same name as that of class.  The constructor is invoked when the object of its associated class is created.  Constructor constructs the values of data members of the class.
  • 4.
    Constructor  A constructoris declared and defined as follows: Class integer { int m, n; public : integer(void); // constructor declared ….. ….. }; integer :: integer(void) // constructor defined { m = 0; n = 0; }
  • 5.
    Characteristics of Constructor The constructor have some special characteristics:  They should be declared in the public section.  They are invoked automatically when the objects are created.  They do not have return type.  They cannot be inherited.  Like other C++ functions, they can have default arguments.  Constructors cannot be virtual.  They make implicit calls to the operators new and delete when memory allocation is required.
  • 6.
    Types of Constructors Default constructor.  Parameterized constructor.  Copy constructor.  Dynamic constructor.
  • 7.
    Default Constructor  Aconstructor that accepts no parameters is called the default constructor.  The default constructor for class A is A::A().  If no such constructor is defined, then the compiler supplies a default constructor.
  • 8.
    Parameterized Constructor  Theconstructor that can take arguments are called parameterized constructors. class integer { int m, n; public: integer(int x, int y); // parameterized constructor ….. ….. }; integer :: integer(int x, int y) { m = x; n = y; }
  • 9.
    Copy Constructor  Acopy constructor can take as argument an object of the same class to which it belongs.  A copy constructor is used to declare and initialize an object from another object. integer I2(I1);  Define the object I2 and at the same time initialize it to the values of I1. integer I2 = I1;
  • 10.
    Multiple constructor ina class class integer { int m, n; public: integer(){ m = 0; n = 0; } // constructor 1 integer(int a, int b) // constructor 2 { m = a; n = b; } integer( integer & i ) // constructor 3 { m = i.m; n = i.n; } };
  • 11.
    Dynamic Constructor  Theconstructors can also be used to allocate memory while creating objects.  Allocation of memory to objects at the time of their construction is known as dynamic construction of objects.  The memory is allocated with the help of the new operator.
  • 12.
    Dynamic Constructor class string { char*name; int length; public: string() { length = 0; name = new char [ length +1]; } string (char *s) { length = strlen(s); name = new char [ length + 1]; strcpy(name, s); } void display(void) { cout << name << “n” ; } void join(string &a, string &b); };
  • 13.
    Dynamic Constructor void string:: join(string &a, string &b) { length = a.length + b.length; delete name; name = new char [ length + 1]; strcpy(name, a.name); strcat(name, b.name); } int main() { char *first = “Mathew”; string name1(first), name2(“Joseph”), s1; s1.join(name1, name2); s1.display(); return 0; }
  • 14.
    Constructor with Default Arguments Constructors can be defined with default arguments.  Eg: complex(float real, float imag=0);  complex c (5.0);  complex c(2.0, 3.0);
  • 15.
    Dynamic Initialization ofObjects  Class objects can be initialized dynamically.  The initial value of object is provided during runtime.  Dynamic initialization can be provided using overloaded constructors.  Example.
  • 16.
    Destructor  A destructoris used to destroy the objects that have been created by a constructor.  It is defined as: ~ integer() { }  A destructor never takes any arguments nor does it return any value.
  • 17.
    Practice Example  Createa class student with the data members roll no, student name and percentage marks. Create a default constructor to initialize each member as zero. Demonstrate all the types of constructors to initialized the objects. Display all the objects.
  • 18.
    Summary  ______ areused to initialize variable and to allocate memory.  Constructor has the same name as that of class. ( True / False )  A constructor that takes arguments is known as __________ .  A _________ is used to declare and initialize an object from another object.  The memory is allocated with the help of the _____ operator.  Objects are destroyed by using ________.
  • 19.
    Short Answer Questions How do we invoke a constructor function?  The constructor is invoked whenever an object of its associated class is created.  List some of the special properties of the constructor.  Properties of constructors
  • 20.
    Short Answer Questions What do you mean by dynamic initialization of objects? Why do we need this?  Dynamic initialization of objects means providing the values to an object at runtime. This provides the flexibility of using different format of data at run time depending upon the situation.
  • 21.
    Short Answer Questions Explain the following statements: time T2 (T1); time T2 = T1; T1 and T2 are objects of time class.  Both the statements are demonstrating the copy constructor. Here the object T2 of class time is created and at the same time it is initialized with the values of another object T1 of class time.
  • 22.
    Short Answer Questions Explain the term constructor overloading.  Constructor overloading means when more than one constructor is defined in a class.  What is default constructor? What happens if no such constructor is defined?  A constructor that accepts no parameters is called the default constructor. If no such constructor is defined, then the compiler supplies a default constructor.
  • 23.
    References  Object OrientedProgramming with C++ by E. Balagurusamy.
  • 24.