SlideShare a Scribd company logo
Contents
oIntroduction
oCharacteristics of Constructor.
oTypes of constructor.
- Default Constructor
- Parameterized Constructor
- Copy Constructor
oOverloaded Constructor
-Multiple Constructor in a class
-Constructor with default argument
oDestructor
oDifference between constructor & destructor.
oConclusion
Concept of constructor
In C++, a constructor is a ‘special’ member function whose task is
to initialize the objects of it’s class when it is created.

It is special because it’s name is the same as the class name.
It is called constructor because it constructs the value of data
members of the class.

The constructor is invoked whenever an object of it’s associated
class is created.
Characteristics of Constructor
They should be declared in the public section.
They are invoked automatically when the objects are
created.
Do not have return types.
Cannot be virtual .
Cannot be inherited through a derived class can call
the base class constructor.
Program to explain concept of
constructor
#include<iostream.h>
#include<conio.h>
class stud
{
int roll_no;
char name[20];
public:
stud();
void show()
{
cout<<“roll_no”<<roll_no<<endl;
cout<<“name”<<name<<endl;
}
};

stude::stud()
{
cout<<“enter roll_no”<<endl;
cin>>roll_no;
cout<<“enter name”<<endl;
cin>>name;
}
void main()
{
stud s;
clrscr();
s.show();
getch();
}
Types of constructor

Constructor

1.Default

2.Parameterized

3.Copy
1.Default Constructor
 A constructor that accepts no parameters is called the default
constructor.
If no such constructor is defined , then the compiler supplies a default
constructor.
E.g.
#include<iostream.h>
class integer
{
int x;
public:
integer();
};
integer::integer()
{
x= 10;
}
void main()
{
integer int 1;
getch();
}
Parameterized constructor
The constructors that can take arguments are as shown
bellow:
class integer
{
int m,n;
public:
integer(intx,inty);//parameterized constructor
------------------------------};
integer::integer(int x,int y)
{
m=x;
n=y;
}
Calling to Constructor
By calling the constructor explicitly
integer int1=integer(0,100);//explicit call
This statement creates an integer object int1 and passes the
values 0 and 100 to it.
By calling the constructor implicitly
integer int1(0,100);//implicit call.
The constructor functions can also be defined as inline
functions,
e.g.
class a
e.g.
{
class integer
------{
------int m,n;
public:
public:
a(a);
integer(int x,int y)
};
{
it is illegal
m=x;
class a
n=y;
{
}
-- - - - ----------------- - - - ---------------public:
};
a(a&);
};
is valid
Copy Constructor
- However

a constructor can accept a reference to it’s own
class as a parameter, in such cases, the constructor is called
the copy constructor.
Syntax:
class integer
{
int x;
public:
integer(integer &i)
{
x=i.x;
}
-- - - };
Program to explain copy constructor
# include<iostream.h>
#include<conio.h>
class integer
{
int x;
public:
integer(int a)
{
x=a;
}
integer(integer &i)
{
x=i.x;
}
void show()
{
cout<<“x=”<<x<<endl;
}
};

void main()
{
integer i1(100);
integer i2(i1);
integer i3=i1
integer i4;
i4=i1; // it is not copy
constr
cout<<“i1”;
i1.show();
cout<<“i2”;
i2.show();
cout<<“i3”;
i3.show();
getch();
}
Overloaded Constructors: Multiple Constructors in a class
- We are used two kinds of
constructors, they are:
1. integer();
//no arguments
2. Integer(int,int); //two arguments
- e.g
class integer
{
int m,n;
public:
integer() // constructor 1
{
m=0;
n=0;
}
integer(int a,int b) //constructor 2
{
m=a;
n=b;
}

integer(integer &i) //constructor3
{
m=i.m;
n=i.n;
}
};
Program shows the use of overloaded constructors
#include<iostream.h>
class integer
{
int m,n;
public:
integer()
{
m=0;
n=0;
}
integer(int a,int b)
{
m=a;
n=b;
}
integer(ineger &x)
{
m=x.m;
n=x.n;
} };

void main()
{
integer i1;
integer i2(30,60);
integer i3(i2);
getch();
}
Constructors with default arguments
- It is possible to define constructors with
default arguments.
- For example, the constructor complex()
can be declared as follows:
complex(float teal, float imag=0);
- e.g. program
#include<iostream.h>
#include<conio.h>
class student
{
int roll_no;
float marks;
public:
student(int x=10;float y=40)
{
roll_no=x;
marks=y;
}

void display()
{
cout<<“roll no”<<roll_no<<endl;
cout<<“marks”<<marks<<endl;
}
};
void main()
{
student s1;
student s2(1,60);
clrscr();
cout<<“output using default
constructor argumets:n”;
s1.display();
cout<<“output without default
constructor arguments:n”;
s2.display();
getch();
}
Advantages of constructor
1. Constructor is invoked automatically as soon as the object of that
class is created this concept is also known as automatic
initialization of objects.
2. A key benefit of using a constructor is that it guarantees that the
object will go through proper initialization before being used.
3. When a user instantiates an object, that object’s constructor is
called and must return before the user can perform any other
work with that object.
4. The main use and advantage of constructors is to initialize objects.
Destructors
- A destructor, as the name implies, is used to destroy the objects that
have been created by a constructor.
- Like a constructor, the destructor is a member function whose name is
the same as the class name but is precede by a tilde ( ~ ).
- E.g.
~integer()
{
----------------------------}
- Destructor never take any arguments.
- The object is destroyed when end of scope of program.
Difference Between Constructor & Destructor
Constructor

Destructor

1.Constructor is a special
member function whose task is
to initialize the object’s of it’s
class.

1.Destructor is used to destroy
objects that have been created
by a constructor.

2. It’s name is same as class
name .

2. it’s name is same as class
name but is preceded by tilde.

3.Constructor is invoked when
the object of it’s associated
class is created.

3.It is invoked implicitly by the
compiler upon exit of the
program to clean up the storage
that is no longer accessible.

4.Constructor can take
arguments.

4. Destructor does not take any
arguments.

5.e.g. student(int a,int b)

5. e.g. ~student()

More Related Content

What's hot

Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
sandeep54552
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloading
Rai University
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
Thooyavan Venkatachalam
 
Constructors
ConstructorsConstructors
Constructors
M Vishnuvardhan Reddy
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ pptKumar
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
Mahender Boda
 
Object oriented concepts ppt
Object oriented concepts pptObject oriented concepts ppt
Object oriented concepts ppt
Prof. Dr. K. Adisesha
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its functionFrankie Jones
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
Abstract class
Abstract classAbstract class
Abstract class
Tony Nguyen
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
BalajiGovindan5
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
Himanshu Kaushik
 

What's hot (20)

Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloading
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
Constructors
ConstructorsConstructors
Constructors
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Object oriented concepts ppt
Object oriented concepts pptObject oriented concepts ppt
Object oriented concepts ppt
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Abstract class
Abstract classAbstract class
Abstract class
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 

Viewers also liked

Java Static Factory Methods
Java Static Factory MethodsJava Static Factory Methods
Java Static Factory Methods
Ye Win
 
Tut Constructor
Tut ConstructorTut Constructor
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
gourav kottawar
 
constructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsconstructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsKanhaiya Saxena
 
Learning c++
Learning c++Learning c++
Learning c++
Bala Lavanya
 
Builder pattern vs constructor
Builder pattern vs constructorBuilder pattern vs constructor
Builder pattern vs constructor
Liviu Tudor
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructorHaresh Jaiswal
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
Saharsh Anand
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
gourav kottawar
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
Learn By Watch
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Hitesh Kumar
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 

Viewers also liked (15)

Java Static Factory Methods
Java Static Factory MethodsJava Static Factory Methods
Java Static Factory Methods
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
constructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsconstructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objects
 
Learning c++
Learning c++Learning c++
Learning c++
 
Builder pattern vs constructor
Builder pattern vs constructorBuilder pattern vs constructor
Builder pattern vs constructor
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructor
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 

Similar to Constructor and desturctor

Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Prof. Dr. K. Adisesha
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
Rassjb
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Sunipa Bera
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
Rokonuzzaman Rony
 
C++
C++C++
C++
C++C++
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
study material
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
LadallaRajKumar
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
RAJ KUMAR
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
Shweta Shah
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
Samad Qazi
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2
Abbas Ajmal
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
Lovely Professional University
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
Lovely Professional University
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects newlykado0dles
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
MadnessKnight
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
akreyi
 

Similar to Constructor and desturctor (20)

Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
C++
C++C++
C++
 
C++
C++C++
C++
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 

Recently uploaded

"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 

Recently uploaded (20)

"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 

Constructor and desturctor

  • 1.
  • 2. Contents oIntroduction oCharacteristics of Constructor. oTypes of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor oOverloaded Constructor -Multiple Constructor in a class -Constructor with default argument oDestructor oDifference between constructor & destructor. oConclusion
  • 3. Concept of constructor In C++, a constructor is a ‘special’ member function whose task is to initialize the objects of it’s class when it is created. It is special because it’s name is the same as the class name. It is called constructor because it constructs the value of data members of the class. The constructor is invoked whenever an object of it’s associated class is created.
  • 4. Characteristics of Constructor They should be declared in the public section. They are invoked automatically when the objects are created. Do not have return types. Cannot be virtual . Cannot be inherited through a derived class can call the base class constructor.
  • 5. Program to explain concept of constructor #include<iostream.h> #include<conio.h> class stud { int roll_no; char name[20]; public: stud(); void show() { cout<<“roll_no”<<roll_no<<endl; cout<<“name”<<name<<endl; } }; stude::stud() { cout<<“enter roll_no”<<endl; cin>>roll_no; cout<<“enter name”<<endl; cin>>name; } void main() { stud s; clrscr(); s.show(); getch(); }
  • 7. 1.Default Constructor  A constructor that accepts no parameters is called the default constructor. If no such constructor is defined , then the compiler supplies a default constructor. E.g. #include<iostream.h> class integer { int x; public: integer(); }; integer::integer() { x= 10; } void main() { integer int 1; getch(); }
  • 8. Parameterized constructor The constructors that can take arguments are as shown bellow: class integer { int m,n; public: integer(intx,inty);//parameterized constructor ------------------------------}; integer::integer(int x,int y) { m=x; n=y; }
  • 9. Calling to Constructor By calling the constructor explicitly integer int1=integer(0,100);//explicit call This statement creates an integer object int1 and passes the values 0 and 100 to it. By calling the constructor implicitly integer int1(0,100);//implicit call.
  • 10. The constructor functions can also be defined as inline functions, e.g. class a e.g. { class integer ------{ ------int m,n; public: public: a(a); integer(int x,int y) }; { it is illegal m=x; class a n=y; { } -- - - - ----------------- - - - ---------------public: }; a(a&); }; is valid
  • 11. Copy Constructor - However a constructor can accept a reference to it’s own class as a parameter, in such cases, the constructor is called the copy constructor. Syntax: class integer { int x; public: integer(integer &i) { x=i.x; } -- - - };
  • 12. Program to explain copy constructor # include<iostream.h> #include<conio.h> class integer { int x; public: integer(int a) { x=a; } integer(integer &i) { x=i.x; } void show() { cout<<“x=”<<x<<endl; } }; void main() { integer i1(100); integer i2(i1); integer i3=i1 integer i4; i4=i1; // it is not copy constr cout<<“i1”; i1.show(); cout<<“i2”; i2.show(); cout<<“i3”; i3.show(); getch(); }
  • 13. Overloaded Constructors: Multiple Constructors in a class - We are used two kinds of constructors, they are: 1. integer(); //no arguments 2. Integer(int,int); //two arguments - e.g class integer { int m,n; public: integer() // constructor 1 { m=0; n=0; } integer(int a,int b) //constructor 2 { m=a; n=b; } integer(integer &i) //constructor3 { m=i.m; n=i.n; } };
  • 14. Program shows the use of overloaded constructors #include<iostream.h> class integer { int m,n; public: integer() { m=0; n=0; } integer(int a,int b) { m=a; n=b; } integer(ineger &x) { m=x.m; n=x.n; } }; void main() { integer i1; integer i2(30,60); integer i3(i2); getch(); }
  • 15. Constructors with default arguments - It is possible to define constructors with default arguments. - For example, the constructor complex() can be declared as follows: complex(float teal, float imag=0); - e.g. program #include<iostream.h> #include<conio.h> class student { int roll_no; float marks; public: student(int x=10;float y=40) { roll_no=x; marks=y; } void display() { cout<<“roll no”<<roll_no<<endl; cout<<“marks”<<marks<<endl; } }; void main() { student s1; student s2(1,60); clrscr(); cout<<“output using default constructor argumets:n”; s1.display(); cout<<“output without default constructor arguments:n”; s2.display(); getch(); }
  • 16. Advantages of constructor 1. Constructor is invoked automatically as soon as the object of that class is created this concept is also known as automatic initialization of objects. 2. A key benefit of using a constructor is that it guarantees that the object will go through proper initialization before being used. 3. When a user instantiates an object, that object’s constructor is called and must return before the user can perform any other work with that object. 4. The main use and advantage of constructors is to initialize objects.
  • 17. Destructors - A destructor, as the name implies, is used to destroy the objects that have been created by a constructor. - Like a constructor, the destructor is a member function whose name is the same as the class name but is precede by a tilde ( ~ ). - E.g. ~integer() { ----------------------------} - Destructor never take any arguments. - The object is destroyed when end of scope of program.
  • 18. Difference Between Constructor & Destructor Constructor Destructor 1.Constructor is a special member function whose task is to initialize the object’s of it’s class. 1.Destructor is used to destroy objects that have been created by a constructor. 2. It’s name is same as class name . 2. it’s name is same as class name but is preceded by tilde. 3.Constructor is invoked when the object of it’s associated class is created. 3.It is invoked implicitly by the compiler upon exit of the program to clean up the storage that is no longer accessible. 4.Constructor can take arguments. 4. Destructor does not take any arguments. 5.e.g. student(int a,int b) 5. e.g. ~student()