Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Constructor And Destructor

Introductio
nA constructor is a special member function whose task is to
initialize the data members of an objects of its class.
 It is special because it has same name as its class name.
 It invokes automatically whenever a new object of its associated
class is created.
 It is called constructor because it constructs the initial values of
data members and build your programmatic object.

2
class test{ Constructor
int x,y; Output :  There is no need to write any
public: test() { X is = 0 statement to invoke the constructor
x =0; Y is = 0 function.
y = 0;  If a ‘normal’ member function is
} defined for initialization, we need to
void output() { invoke that function for each and
every objects separately.
cout<<"\X is = "<<x;
cout<<"\nY is = "<<y; o Implicit call (shorthand method)
}  test t();
};
o Explicit call
int main(){
 test t;
test t();
t.output(); t = test();
} 3
Characteristic
s
 They must be declared in the public scope.
 They are invoked automatically when the objects are created.
 They do not have return types, not even void and they cannot return
values.
 They cannot be inherited, though a derived class can call the base class
constructor.
 Like other C++ functions, Constructors can have default arguments.
 Constructors can not be virtual.
 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 4
Types of
Constructor
1. Default Constructor/Non-Arg Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Constructor overloading
5. Dynamic Constructor

5
Default
Constructor
 A constructor without any parameter / no parameters is known as non-
arg constructor or simply default constructor.

 If no constructor is defined in a class, then compiler implicitly


provides an empty body constructor which is called as default
constructor.

6
class sample
{ class sample
int a; {
public: int a;
void display () public :
{ sample()
.. {
} After }
Compilatio
}; n
void display ()
{
int main (){ ..
}
sample s(); };
s.display();
Compiler has implicitly added a constructor to the class, which has
return 0; empty body, because compiler is not supposed to put any logic in that.
}
7
class test{
int x,y; Output :
After
public: test() { X is = 0
Compilation
x =0; Y is = 0
y = 0;
}
void output() {
cout<<"\X is = "<<x;
cout<<"\nY is = "<<y;
}
}; Non-Arg (Default) constructor,
int main(){ which takes no arguments
test t();
t.output();
}
Parameterised
Constructors
 Sometimes it becomes necessary to initialize the various data elements
of an 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.

9
class test { Output :
int a,b; The Numbers are : 3 2
public: test(int x ,int y) {
a=x;
b=y;
}
void show() {
cout<<"The Numbers are : "<<a<<" "<<b;
}
};
int main(){
test t(3,2);
Parametirised constructor, which
t.show();
takes 1 arguments as two numbers.
return 0;
}
Copy
Constructor
 A reference variable has been used as an argument to the copy
constructor.
A cannot
 We copy constructor is used tobydeclare
pass the argument and
value to initialize
a copy an object from
constructor.
another object.
EX:
test(test &i1) ;
test t2(t1) ; or test t2=t1 ;
The process of initializing through a copy constructor is known as
copy initialization.
If t1 and t 2 are objects, this statement is legal and assigns the values of
t1 to t 2, member-by-member.
The statement t 2 = t1;
will not invoke the copy 11
class test { int a, b;
public: test(int x, int y) { Output :
a = x; I'm Copy Constructor
b = y; Values : 10 20
} Values : 10 20
test(const test& t1) {
a = t1.a;
b = t1.b;
cout << "\nI'm Copy Constructor";
}
void display() {
cout << "\nValues :" << a << "\t" << b;
}
};
int main() {
test t1(10, 20);
test t2(t1);
t1.display(); t2.display(); }
Constructors Overloading /
class test {
Multiple constructor in a class
int b; C + + permits to use more than one
double y; constructors in a single class.
public: test(int a) {
b = a;
cout<<"\nThe Integer Value is : "<<b;
}
test(double x) {
y = x;
cout<<"\nThe Floating Value is : "<<y;
}
};
int main() {
test t1(2.7);
test t2(7);
}
Dynamic
Constructors
 The constructors can also be used to allocate memory while creating
objects.
 This will enable the system to allocate the right amount of memory for each
object when the objects are not of the same size.
 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.
 Providing initial value to objects at run time.
Advantages:
We can provide various initialization formats, using overloaded
constructors.
This provides the flexibility of using different format of data at run
time depending upon the situation.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 14
class test{
int *a; Output :
public:test(){ The value of object o1's a is : 10
a=new int;
*a=10;
The value of object o1's a is : 9
}
test(int b) {
a=new int;
*a=b;
}
int output() {
return(*a);
}
};
int main() {
test o1, o2(9);
cout<<"The value of object o1's a is:";
cout<<o1.output();
cout<<"\nThe value of object 02's a is:"<<o2.output();
return 0;
}
Destructor
 sA destructor is used to destroy the objects that have been created by a
constructor.
 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.
 It is a good practice to declare destructors in a program since it releases
memory space for further use.
 Like constructor, the destructor is a member function whose name is the
same as the class name but is preceded by a tilde.
eg: ~ test( ) { }

 Whenever new is used to allocate memory in the constructor, we should


use delete to free that memory.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 16
class test{
public: int a,b;
test(){ // This is the constructor declaration
a=5; b=10;
cout << "Object is being created" << endl;
}
void output(){
cout<<“ a : "<<a<<endl<<" b : "<<b<<endl;
}
~test(){ // This is the destructor declaration
cout << "Object is being deleted" << endl;
} Output :
};
int main( ){ Object is being created       
test t1;  a : 5                                        
t1.output();  b : 10                                      
return 0; Object is being deleted 
}

You might also like