Object Oriented Programming
Constructor & Destructor in C++
BSCS(2nd)
By: Syeda Zareen Fatima
What is Constructor???
 Constructor is a Special type of function which has
the same name as the class
 Constructor is being called automatically at the
time of object declaration
 The return type of the constructor is the same as
the type of the class
What is Destructor???
 Destructor is a special member function that is
executed automatically.
 An object is destroyed by destructor which was
created by the constructor.
 Destructors are used to de-allocate the memory
that has been allocated for the object by
constructor.
 A destructor declaration should always begin with
the tidle( ) symbol.
Ex. of Constructor and Destructor
#include<iostream.h>
#include<conio.h>
Class test
{
Public:
Test()
{int n=10;
Cout<<n;
}
--Test()
{
Cout<<“object destroyed”;
}
};
void main()
{
test obj;
getch();
}
Types of Constructor
 Default Constructor
 Parametric Constructor
 Copy Constructor
Why we use Constructors in C++
 Constructor is a specialized member function of class which is used to
create and initialize objects.
Features
 A constructor can only have one access modifier which is public
 A constructor is never inherited & Overriden
 Each and every c++ class has constructor, either it is provided by
compiler by default or explicitly created by user.
Example
#include<stdio.h>
#include<iostream.h>
class A
{
public:
Int a;
A() //constructor
{
a=100;
}
Void show()
{
cout<<a;
}};
void main()
{
A obj;
Obj.show();
getch();
}
Difference b/w Constructor & Destructor
Constructor Destructor
It allocates memory to an object It de-allocates memory of an object
The name of the constructor is the
same as the class name
The name of destructor is same as
class name but precodint tildo sign.
It is being automaticaly called at the
time of object declaration
It is automatically called at the time
of object termination
We can pass arguments through
constructor
We can not pass arguments through
destructor
We can declare multiple onstructors
in a class
Only one destructor is allowed in
class
Difference b/w Constructor & Destructor
Constructor Destructor
Constructor can be overloaded Destructor can not be overloaded
Constructor has various types
(default,parametrized, copy)
Destructor has no various types
Syntax
Classname(parameter optional)
{
________
________
____
}
Syntax
classname()
{
________
________
______
}
What is Default Constructor?
A constructor that accepts no parameters is called default constructor.
Syntax
class-name()
{
________ // code
_________ // code
}
Example of Default Constructor
#include<iostream.h>
#include<conio.h>
Class A
{ int a;
Public:
A()
{
a=100;
Cout<<a;
}
};
void main()
{
clrscr();
A obj;
getch();
}
What is Parametrized Constructor?
A constructor that accets or receives parameters is called parametrized
constructor.
#include<iostream.h>
#include<conio.h>
Class A
{
Public:
Int a,b;
A(int x, int y)
{ a=x;
b=y;
}
Void show()
{cout<<a<<“ “<<b;
}};
Void main(){
Clrscr();
A obj(10,20);
Obj.show();
getch();
}
What is Copy Constructor?
A constructor that is used to copy or initialize the value of one object
into another object is called copy constructor.
Syntax
Class-name(class-name & ref)
{
________ //code
________ //code
}
Example
#include<iostream.h>
#include<conio.h>
Class A
{
Int a,b;
Public:
A(int x,int y)
{
a=x;
b=y;
}
A(A & ref)
{
a=ref.a;
b=ref.b;
}
Void show()
{
Cout<<a<<“ “<<b<<endl;
}
};
Void main()
{
A obj(10,20);
A obj2=obj;
Obj.show();
getch();
}

Constructor and Destructor in c++

  • 1.
    Object Oriented Programming Constructor& Destructor in C++ BSCS(2nd) By: Syeda Zareen Fatima
  • 2.
    What is Constructor??? Constructor is a Special type of function which has the same name as the class  Constructor is being called automatically at the time of object declaration  The return type of the constructor is the same as the type of the class
  • 3.
    What is Destructor??? Destructor is a special member function that is executed automatically.  An object is destroyed by destructor which was created by the constructor.  Destructors are used to de-allocate the memory that has been allocated for the object by constructor.  A destructor declaration should always begin with the tidle( ) symbol.
  • 4.
    Ex. of Constructorand Destructor #include<iostream.h> #include<conio.h> Class test { Public: Test() {int n=10; Cout<<n; } --Test() { Cout<<“object destroyed”; } }; void main() { test obj; getch(); }
  • 5.
    Types of Constructor Default Constructor  Parametric Constructor  Copy Constructor
  • 6.
    Why we useConstructors in C++  Constructor is a specialized member function of class which is used to create and initialize objects. Features  A constructor can only have one access modifier which is public  A constructor is never inherited & Overriden  Each and every c++ class has constructor, either it is provided by compiler by default or explicitly created by user.
  • 7.
    Example #include<stdio.h> #include<iostream.h> class A { public: Int a; A()//constructor { a=100; } Void show() { cout<<a; }}; void main() { A obj; Obj.show(); getch(); }
  • 8.
    Difference b/w Constructor& Destructor Constructor Destructor It allocates memory to an object It de-allocates memory of an object The name of the constructor is the same as the class name The name of destructor is same as class name but precodint tildo sign. It is being automaticaly called at the time of object declaration It is automatically called at the time of object termination We can pass arguments through constructor We can not pass arguments through destructor We can declare multiple onstructors in a class Only one destructor is allowed in class
  • 9.
    Difference b/w Constructor& Destructor Constructor Destructor Constructor can be overloaded Destructor can not be overloaded Constructor has various types (default,parametrized, copy) Destructor has no various types Syntax Classname(parameter optional) { ________ ________ ____ } Syntax classname() { ________ ________ ______ }
  • 10.
    What is DefaultConstructor? A constructor that accepts no parameters is called default constructor. Syntax class-name() { ________ // code _________ // code }
  • 11.
    Example of DefaultConstructor #include<iostream.h> #include<conio.h> Class A { int a; Public: A() { a=100; Cout<<a; } }; void main() { clrscr(); A obj; getch(); }
  • 12.
    What is ParametrizedConstructor? A constructor that accets or receives parameters is called parametrized constructor. #include<iostream.h> #include<conio.h> Class A { Public: Int a,b; A(int x, int y) { a=x; b=y; } Void show() {cout<<a<<“ “<<b; }}; Void main(){ Clrscr(); A obj(10,20); Obj.show(); getch(); }
  • 13.
    What is CopyConstructor? A constructor that is used to copy or initialize the value of one object into another object is called copy constructor. Syntax Class-name(class-name & ref) { ________ //code ________ //code }
  • 14.
    Example #include<iostream.h> #include<conio.h> Class A { Int a,b; Public: A(intx,int y) { a=x; b=y; } A(A & ref) { a=ref.a; b=ref.b; } Void show() { Cout<<a<<“ “<<b<<endl; } }; Void main() { A obj(10,20); A obj2=obj; Obj.show(); getch(); }