Object Constructor & Destructor
Object Constructor & Destructor
Chapter 4
Contents
Introduction to Constructor
Parameterized Constructor
Copy Constructor
Destructor
2
Introduction: Constructor
• C++ provides a built-in special type of member
function
– used to construct object.
– compiler automatically execute these function.
– Programmer does not need to make any effort for invoking
these function.
– When a object is created , constructor is executed.
– Programmer can call the constructor explicitly also.
3
Characteristics of Constructor
• A constructor is a member function (manager function) that is executed
automatically whenever an object is created.
• In other words a constructor is special member function due to which
automatic initialization is carried out (f an object).
• It is a convenient way to initialize an object when it is first created without
the need to make a separate call to a member function
Characteristics:
• Constructor has the same name as that of class it belongs.
• Constructor is executed when a object is declared.
• Every class has an implicit constructor when not defined
• Constructor have no return type, not even void.
• Job of the constructor is to initialize objects and allocate appropriate
memory to the object.
• though constructor are executed implicitly, it can be executed explicitly.
• Constructor can be overloaded.
4
Types of Constructor
1. Default Constructor
2. Parameterized Constructor (Constructor with
Argument)
3. Constructor with Default Argument
4. Copy Constructor
5
Default Constructor
• Constructor with out argument is called
Default Constructor.
6
Example 1
#include<iostream> void num::display()
using namespace std; {
class num
cout<<a<<endl<<b<<endl;
{
int a; }
int b;
public: main()
num(); // declaration of Default {
// Constructor num num n1; // implicit call to Default
void display(); // Constructor num
}; n1.display();
num n2=num(); // explicit call to
num::num() // declaration of default //Default Constructor num
//Constructor num outside the class
{ n2.display();
a=10; return 0;
b=20; }
}
7
Example 2
#include<iostream> int main ( )
using namespace std;
class integer {
{ integer int1(0, 100); //constructor called implicitly
int m,n; integer int2=integer (25,75); //constructor called explicitly
public:
integer (int, int);
cout<<"\nOBJECT 1"<<"\n";
void display(void) int1.display();
{ cout<<"\nOBJECT 2"<<"\n“;
cout<<"m="<<m<<"\n"; int2.display ( );
cout<<"n="<<n<<"\n";
} return 0;
}; }
integer::integer (int x,int y)
{
m=x;
n=y;
}
8
Parameterized Constructor
• The constructors that can take arguments are
called parameterized constructors.
• This feature in C++ initializes the various data
members of different objects with different
values (instead of initialization of the data
members of all objects to zero) when they are
created.
9
Example
#include<iostream> void num::display()
using namespace std; {
class num cout<<a<<endl<<b<<endl;
{ }
int a;
int b; main()
public: {
num(int x,int y); // declaration of Parameterized num n1(10,20); // implicit call to Parameterized Constructor num
// Constructor num ( constructor ) n1.display();
void display(); num n2=num(30,40); // explicit call to Parameterized Constructor num
}; n2.display();
return 0;
}
num::num(int x, int y) // definition of Parameterized
Constructor num
{
a=x;
b=y;
}
10
Example 2
class myclass •constructor function can be defined as inline
function.
int m,n; •Parameters of a constructor can be of any type
except that of the class to which it belong
public:
myclass (int x,int y); // parameterized constructor e.g. class X
{
…….. ………….
……. ………….
public:
……. X (X); //class X as a parameter is illegal
};
};
•However, a constructor can accept a reference to
int main( ) its on class as a parameter
class X
{ {
myclass c1; // may not work ………
………
myclass c2(20,40); //implicit public:
} X (X&); //reference of class X as a parameter.
//This constructor is called copy constructor.
}
11
Constructor with Default Argument
• In this type of constructor we can assign
default value to argument of the Constructor.
12
Example
#include<iostream> main()
using namespace std; {
num n1(10,20); // implicit call to constructor num with two argument
class num
n1.display();
{ num n2(30); // implicit call to constructor num with one argument
int a; n2.display();
int b; num n3; // implicit call to constructor num with no argument
n3.display();
public:
num(int x=1,int y=2); // declaration of constructor num n4=num(40,50); // explicit call to constructor num with two argument
//num (with default argument)
n4.display();
void display(); num n5=num(60); // explicit call to constructor num with one argument
}; n5.display();
num n6=num(); // explicit call to constructor num with no argument
n6.display();
num::num(int x,int y) // definition of constructor num return 0;
{ }
a=x;
b=y;
}
void num::display()
{
cout<<a<<endl<<b<<endl;
}
13
Copy Constructor
• Copy Constructor creates a copy of other object .
• Here it is possible to pass reference of object to the constructor.
• Using copy constructor is possible for the programmer to declare and
initialize one object using reference of another object. when the
constructor is called a copy of an object is created.
• A copy constructor is used to declare and initialize an object from
another object.
• A reference variable is used as an argument to the copy constructor.
• We can not pass the argument by value to a copy constructor, this is
because when an argument is passed by value, a copy of it is
constructed i.e. a copy constructor is constructed.
• It calls itself over and over until the compiler runs out of memory. So,
in the copy constructor the argument must be passed by reference
which creates no copies.
14
Example
#include<iostream> void num::display()
using namespace std; {
class num cout<<a<<endl<<b<<endl;
{
}
int a;
int b;
public:
num(int x,int y); // declaration of main()
//Parameterized constructor num {
num(num &n); // declaration of Copy constructor num num n1(10,20);// implicit call to Parameterized Constructor num
void display(); n1.display();
}; num n2(n1); // implicit call to Copy Constructor num
num::num(num &n) // definition of Copy Constructor num
n2.display();
{
num n3=n1; // explicit call to Copy Constructor num
a=n.a;
b=n.b; n3.display();
} return 0;
num::num(int x,int y) // definition of Parameterized }
//Constructor num
{
a=x;
b=y;
}
15
Copy Constructor contd…..
e.g. if the class name is alpha
alpha a1; //object created for class alpha
alpha a3 (a1); //copy initialization i.e. object a3 is defined and at the
sae time it is initialized to the values of a1
alpha a3=a1; //alternate syntax
alpha (alpha &a1); //copy constructor
16
Copy Constructor contd…..
To prevent this member wise copy it is necessary to provide the copy constructor.
The member wise copy which the compiler does is dangerous if the object contains pointer.
e.g.
class alpha
{
private:
char *myname;
public:
alpha( )
{
strcpy (myclass, “\0”);
};
alpha a1;
creates object a1, with a pointer named “myname” pointing at a memory location (x), which is copied
from a1.
Due to the member wise copy, both objects have pointers pointing at the same memory location
resulting in one object being dependent on the other. Therefore, it is necessary to provide copy
constructor in this class.
17
Copy Constructor: Example 2
#include <iostream> int main ( )
using namespace std; {
alpha a(100); //object is created and initialized
class alpha alpha b(a); // copy constructor called
alpha c=a; // copy constructor called
{ alpha d;
int id; d = a; //copy constructor not called
public: cout<<"\n Id of a:";
alpha( ){ } //constructor a.display ( );
cout<<"\n Id of b:";
alpha (int a) b.display ( );
{ cout<<"\n Id of c:";
id = a; c.display ( );
} cout<<"\n Id of d:";
d.display ( );
alpha (alpha &x) //copy construtor return 0;
{
id = x.id; }
}
Output:
void display(void) Id of a: 100
{ Id of b: 100
cout<<id; Id of c: 100
} Id of d: 100
};
18
Overloading Constructor
When a class contains more then one constructor that is called overloading
constructor. Like function , also it is possible to overload constructor.
Example:
#include<iostream>
using namespace std;
class num
{
int a;
int b;
public:
num(); // Default Constructor
num(int x,int y); // Parameterized Constructor (Constructor having arguments)
num(int x,int y,int z); // Parameterized Constructor (Constructor having arguments)
num(num &n); // Copy Constructor
void display()
{
cout<<a<<” ”<<b<<endl;
}
};
num::num() // Definition of Default Constructor
{
a=10;
b=20;
}
19
Overloading Example contd…..
num::num(int x,int y) // Definition of Parameterized Constructor having 2 argument
{
a=x;
b=y;
}
num::num(int x,int y,int z) // Definition of Parameterized Constructor having 3 argument
{
a=x;
b=y+z;
}
20
Overloading contd…
main()
{
num n1; // implicit call to default constructor
num n2(30,40); // implicit call to parameterized constructor
num n3(100,200,300); // implicit call to Parameterized constructor
num n4(n2); // implicit call to copy constructor
n1.display();
n2.display();
n3.display();
n4.display();
return 0;
}
21
Destructor
• This is activated when the object is destroyed automatically
when the program ends of when the program controls
passes to a statement outside the cope of the object.
• A destructor has the same name as the class preceded by a
tilde (~) e.g. ~counter ( ).
• Destructors also have no return values. They take no
arguments. Therefore it can not be overloaded.
• Like constructor destructor is also a special member
function.
• The use of these functions is to free memory allocated for
the object by the constructor.
• For example, if an object is used as local variable in a
function, the destructor is invoked when the compiler
returns to the calling procedure.
22
Destructor contd….
• An object can be destroyed only when the resources of the object is
free i.e. first member elements associated with object should be
destroyed then only object will e destroyed by the destructor.
Otherwise system will crash. Therefore before destroying an object,
the resources associated with the object most be destroyed, so we
explicitly call destructor to destroy resources occupied by the object
(a default destructor only destroys object but not resources).
• Since, constructors and destructors will not be robust enough for
our class we should provide our own version of them.
• They can not have return value (not even void).
• Pointers and references can not be used on them (it is not possible
to get their address).
• They can not be declared static, constants or volatile.
• They can not be declared with the keyword virtual.
23
#include <iostream>
Example
using namespace std;
int main( )
class myclass {
{ myclass a1;
int num;
public: myclass b1(5);
myclass( ) // default constructor a1.getdata ( ); //enters new value
{ a1.dispdata ( ); //displays initialized value
num=0;
} b1.getdata ( ); //enters new value
myclass (int n) //overload constructor b1.dispdata ( ); //displays initialized value
{ return 0;
num=n;
} }
~ myclass ( )
{
cout<<"\n object destroyed !";
}
void getdata( )
{
cout<<endl<<"enter an integer:";
}
void dispdata( )
{
cout<<endl<<"Num:"<<num;
}
};
24