SlideShare uma empresa Scribd logo
1 de 15
Constructor & Destructor
Constructors
• Def- A constructor is a special member
function that is a member of a class and
has same name as that class.
• Use- It is used to initialize the object of the
class type with a legal initial value.
Special Characteristics of Constructors
1. These are called automatically when the objects are
created.
2. All the objects of the class having a constructor are
initialized before some use.
3. These should be declared in the public section for
availability to all the functions.
4. Constructor has no return type even void.
5. They can not be inherited.
6. These cannot be static.
7. Default and copy constructor are generated by the
compiler whenever required. Generated constructors
are public.
8. Constructors can have default argument as other C++
functions.
Declaration and Definition
• It can be defined either inside the class
definition or outside the class definition
class X { int i ;
public:
int j,k ;
X() { i = j = k =0;}
};
class X { int i ;
public:
int j,k ;
X() { i = j = k =0;}
};
X::X()
{
I = j = k =0;
}
Note
• Generally a constructor should be defined
under the public section of a class, so that
its object can be created in any function
class X { int i ;
X() { i = j = k =0;}
public:
int j,k ;
void check(void); // member function
};
void X :: check (void)
{
X obj1; //valid
}
int main()
{
X obj2; //invalid
}
Default constructor
• A constructor that accept no parameter is
called the default constructor.
Copy Constructor
• A copy Constructor is used to initialize an object
from another object.
• If you have not defined a copy constructor, the
complier automatically creates it and it is public
• Copy constructor always takes argument as a
reference object.
Consider the following class definition
class sample {
int i, j;
public:
sample ( int a, int b)
{
i = a;
j = b;
}
sample ( sample & s)
{
i = s.i ;
j = s.j ;
}
void print(void)
{
cout<<i <<“ “<<j <<“n”;
}
Above constructors may be used as
follows
sample s1 (10,12); //1st const. called
sample s2 (s1) // copy const. called
sample s3 = s1; copy const. called
Home work
• In a class does not define a constructor,
what will be the initial value of its object.
• What are the significance of default
constructor.
• How many situations when a copy
constructor is automatically called.
Default Arguments
• Just like any other function a constructor
can also have default arguments
• This constructor is equivalent to default
constructor. Because its also allows us to
create objects without any value provided.
• For example
class A
{
int i,j;
public:
X(int x=10,int y=20);
};
A::X(int x,int y)
{
i=x;
j = y;
}
int main()
{
A obj1;
A obj2(250);
A obj3(2,4);
getch();
return 0;
}
i=10
j=20
i=250
j=20
i=2
j=4
obj1 obj2 obj3
Order of Constructor Invocation
• The objects are constructed in the order they are
defined. Consider the following statement
• Sample s1,s2,s3; // the order of construction is
s1,s2,s3.
• But when a class containing objects of another
class as its members. In such a case, the
constructor for member objects are invoked first
and then only, the constructor for containing
class is invoked.
• For example
class A
{
public:
A()
{
cout<<“Constructing A”
<<endl;
}};
class B
{
public:
B()
{
cout<<“Constructing B”
<<endl;
}};
class C
{
private:
A objA;
B objB;
public:
C()
{
cout<<“Constructing C”
<<endl;
}};
int main()
{
clrscr();
C objC;
getch();
}

Mais conteúdo relacionado

Mais procurados

Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Asfand Hassan
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java pptkunal kishore
 
vb.net Constructor and destructor
vb.net Constructor and destructorvb.net Constructor and destructor
vb.net Constructor and destructorsuraj pandey
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsVineeta Garg
 
constructor and destructor
constructor and destructorconstructor and destructor
constructor and destructorVENNILAV6
 
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 2Abbas Ajmal
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructorSaharsh Anand
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop Samad Qazi
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programmingAshita Agrawal
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++Bhavik Vashi
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructorHaresh Jaiswal
 

Mais procurados (20)

Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
Constructor
ConstructorConstructor
Constructor
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
vb.net Constructor and destructor
vb.net Constructor and destructorvb.net Constructor and destructor
vb.net Constructor and destructor
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
constructor and destructor
constructor and destructorconstructor and destructor
constructor and destructor
 
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 & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
 
C# Constructors
C# ConstructorsC# Constructors
C# Constructors
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructor
 

Destaque

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 javaHitesh Kumar
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismrattaj
 
Overloading in java
Overloading in javaOverloading in java
Overloading in java774474
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)Ritika Sharma
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerLuminary Labs
 
Aae oop xp_07
Aae oop xp_07Aae oop xp_07
Aae oop xp_07Niit Care
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++HalaiHansaika
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocationKumar
 

Destaque (18)

Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
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
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 
Overloading in java
Overloading in javaOverloading in java
Overloading in java
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 
Aae oop xp_07
Aae oop xp_07Aae oop xp_07
Aae oop xp_07
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocation
 

Semelhante a Constructor & Destructor

Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorSunipa Bera
 
CST 203 Lecture 7.pptx
CST 203 Lecture 7.pptxCST 203 Lecture 7.pptx
CST 203 Lecture 7.pptxDrKalkaDubey1
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdfMadnessKnight
 
Chapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxChapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxKavitaHegde4
 
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfChapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfKavitaHegde4
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptxRassjb
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxsasukeman
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructorrajshreemuthiah
 
Introductions to Constructors.pptx
Introductions to Constructors.pptxIntroductions to Constructors.pptx
Introductions to Constructors.pptxSouravKrishnaBaul
 
Constructor destructor.ppt
Constructor destructor.pptConstructor destructor.ppt
Constructor destructor.pptKarthik Sekar
 
Constructor&amp; destructor
Constructor&amp; destructorConstructor&amp; destructor
Constructor&amp; destructorchauhankapil
 

Semelhante a Constructor & Destructor (20)

Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
CST 203 Lecture 7.pptx
CST 203 Lecture 7.pptxCST 203 Lecture 7.pptx
CST 203 Lecture 7.pptx
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
 
constructor.ppt
constructor.pptconstructor.ppt
constructor.ppt
 
Chapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxChapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptx
 
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfChapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdf
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
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
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
106da session5 c++
106da session5 c++106da session5 c++
106da session5 c++
 
Introductions to Constructors.pptx
Introductions to Constructors.pptxIntroductions to Constructors.pptx
Introductions to Constructors.pptx
 
Constructor destructor.ppt
Constructor destructor.pptConstructor destructor.ppt
Constructor destructor.ppt
 
Constructor&amp; destructor
Constructor&amp; destructorConstructor&amp; destructor
Constructor&amp; destructor
 

Último

Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 

Último (20)

Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 

Constructor & Destructor

  • 2. Constructors • Def- A constructor is a special member function that is a member of a class and has same name as that class. • Use- It is used to initialize the object of the class type with a legal initial value.
  • 3. Special Characteristics of Constructors 1. These are called automatically when the objects are created. 2. All the objects of the class having a constructor are initialized before some use. 3. These should be declared in the public section for availability to all the functions. 4. Constructor has no return type even void. 5. They can not be inherited. 6. These cannot be static. 7. Default and copy constructor are generated by the compiler whenever required. Generated constructors are public. 8. Constructors can have default argument as other C++ functions.
  • 4. Declaration and Definition • It can be defined either inside the class definition or outside the class definition class X { int i ; public: int j,k ; X() { i = j = k =0;} };
  • 5. class X { int i ; public: int j,k ; X() { i = j = k =0;} }; X::X() { I = j = k =0; }
  • 6. Note • Generally a constructor should be defined under the public section of a class, so that its object can be created in any function
  • 7. class X { int i ; X() { i = j = k =0;} public: int j,k ; void check(void); // member function }; void X :: check (void) { X obj1; //valid } int main() { X obj2; //invalid }
  • 8. Default constructor • A constructor that accept no parameter is called the default constructor.
  • 9. Copy Constructor • A copy Constructor is used to initialize an object from another object. • If you have not defined a copy constructor, the complier automatically creates it and it is public • Copy constructor always takes argument as a reference object. Consider the following class definition
  • 10. class sample { int i, j; public: sample ( int a, int b) { i = a; j = b; } sample ( sample & s) { i = s.i ; j = s.j ; } void print(void) { cout<<i <<“ “<<j <<“n”; } Above constructors may be used as follows sample s1 (10,12); //1st const. called sample s2 (s1) // copy const. called sample s3 = s1; copy const. called
  • 11. Home work • In a class does not define a constructor, what will be the initial value of its object. • What are the significance of default constructor. • How many situations when a copy constructor is automatically called.
  • 12. Default Arguments • Just like any other function a constructor can also have default arguments • This constructor is equivalent to default constructor. Because its also allows us to create objects without any value provided. • For example
  • 13. class A { int i,j; public: X(int x=10,int y=20); }; A::X(int x,int y) { i=x; j = y; } int main() { A obj1; A obj2(250); A obj3(2,4); getch(); return 0; } i=10 j=20 i=250 j=20 i=2 j=4 obj1 obj2 obj3
  • 14. Order of Constructor Invocation • The objects are constructed in the order they are defined. Consider the following statement • Sample s1,s2,s3; // the order of construction is s1,s2,s3. • But when a class containing objects of another class as its members. In such a case, the constructor for member objects are invoked first and then only, the constructor for containing class is invoked. • For example
  • 15. class A { public: A() { cout<<“Constructing A” <<endl; }}; class B { public: B() { cout<<“Constructing B” <<endl; }}; class C { private: A objA; B objB; public: C() { cout<<“Constructing C” <<endl; }}; int main() { clrscr(); C objC; getch(); }