SlideShare una empresa de Scribd logo
1 de 24
Descargar para leer sin conexión
Constructor and
Destructor
1 . C O N S T R U C TO R
2. DEFAULT CONSTRUCTOR

3. PARAMETERIZED CONSTRUCTOR
4. COPY CONSTRUCTOR
5. OVERLOADING CONSTRUCTOR
6 . D E S T R U C T OR

Compiled By: Kamal Acharya
Constructor
 Special member function to initialize

the objects of its class.
 Its name is same as the class name.
 It is invoked whenever the object is
created.
 It construct the values of data members
of the class so it is named as the
constructor.
Compiled By: Kamal Acharya
 Example

Class integer
{
int m,n;
public:
integer(); // constructor
declared
………
};

Compiled By: Kamal Acharya

Constructor defination
integer:: integer()
{
m=0;
n=0;
}
 When object is created from class from

class integer, it will be initialized
automatically.
 Eg. integer int1;
 Here not only int1 is created but also its
data members m and n are initialized to
zero.
Compiled By: Kamal Acharya
Characteristics of Constructor
 They should be declared in the public section.
 They are called automatically when the object are

created.
 They do not have return type even void.
 They have same name as the class name.
 They can have default arguments as the other
function.

Compiled By: Kamal Acharya
Default Constructor
 They takes no parameters.
 They are called internally by the compiler

whenever the object are created.
 There is no need to call it explicitly.

Compiled By: Kamal Acharya
Sample Program
#include<iostream.h>
#include<conio.h>
class integer
{
int m,n;
public:
integer()
{
m=0;
n=0;
}
Compiled By: Kamal Acharya

void display()
{
cout<<"m= "<<m<<" and n=
"<<n;
}
};
void main()
{
clrscr();r
integer int1;
int1.display();
getch();
}
OUTPUT

Compiled By: Kamal Acharya
Parameterized Constructor
 These are the constructor that take arguments.
 They initialized the object data members by the value

which is passed as arguments.
 They are invoked when we pass the arguments to the
object when they are being defined.
 Example: integer int1(2,5);

Compiled By: Kamal Acharya
Sample Program
#include<iostream.h>
#include<conio.h>
class integer
{
int m,n;
public:
integer(int x, int y)
{
m=x;
n=y;
}
Compiled By: Kamal Acharya

void display()
{
cout<<"m= "<<m<<"
and n= "<<n;
}
};
void main()
{
clrscr();
integer int1(5,6);
int1.display();
getch();
}
OUTPUT

Compiled By: Kamal Acharya
Copy Constructor
 It is used to declare and initialized an object from

another object.
 It takes reference to an object of the same class as
itself as an arguments.
 Example:
integer I1;
integer I2(I1);

Compiled By: Kamal Acharya
Sample Program
#include<iostream.h>
#include<conio.h>
class integer
{
int m,n;
public:
integer(integer &x)
{
m=x.m; n=x.n;
}

integer()
{
m=100; n=100;
}
void display()
{
cout<<"m= "<<m<<"
and n= "<<n<<endl;
}
};

Compiled By: Kamal Acharya
void main()
{
clrscr();
integer int1;
int1.display();
integer int2(int1);
int2.display();
getch();
}
Compiled By: Kamal Acharya
OUTPUT

Compiled By: Kamal Acharya
Overloading Constructor
 Constructor overloading is the process of defining

more than one constructor in the same class.
 C++ permits us to use multiple constructor in the
same class.

Compiled By: Kamal Acharya
Sample Program
#include<iostream.h>
#include<conio.h>
class integer
{
int m,n;
public:
integer(integer &x)
{
m=x.m;
n=x.n;
}
Compiled By: Kamal Acharya

integer()
{
m=0;
n=0;
}
integer(int x, int y)
{
m=x;
n=y;
}
void display()
{
cout<<"m= "<<m<<"
and n= "<<n<<endl;
}
};

Compiled By: Kamal Acharya

void main()
{
clrscr();
integer int1;
integer int2(400,500);
integer int3(int2);
int1.display();
int2.display();
int3.display();
getch();
}
OUTPUT

Compiled By: Kamal Acharya
Destructor
 It is used to destroy the objects created by the

constructor.
 It is called for the class object whenever it passes the
scope in the program.
 Whenever new is used in the constructor to allocate
the memory delete should be used in the destructor
to free the memory for future use.

Compiled By: Kamal Acharya
Characteristics of Destructor
 It has same name as the class name but is preceded

by tilde (~) sign.
 It has no return type and do not take any arguments.
 It can not be overloaded.
 It is called whenever the object get out of its scope.

Compiled By: Kamal Acharya
Sample Program
#include<iostream.h>
class integer
{
int m,n;
public:
integer()
{
m=0;
n=0;
cout<<"Default
Constructor is
called"<<endl;
Compiled By: Kamal Acharya
}

integer(int x, int y)
{
m=x;
n=y;
cout<<"Parameterize
d Constructor is
called"<<endl;
}
~integer()
{
cout<<"Object is
detroyed"<<endl;
}
void display()
{
cout<<"m=
"<<m<<" and n=
"<<n<<endl;
}
};

Compiled By: Kamal Acharya

void main()
{
clrscr();
{
integer int1;
int1.display();
}
{
integer int2(2,5);
int2.display();
}
}
OUTPUT

Compiled By: Kamal Acharya

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Inline function
Inline functionInline function
Inline function
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Functions in c
Functions in cFunctions in c
Functions in c
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
5bit field
5bit field5bit field
5bit field
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructor
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 

Destacado

constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programmingAshita Agrawal
 
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
 
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
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPTShubham Mondal
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++Learn By Watch
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructorSaharsh Anand
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++gourav kottawar
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend classAbhishek Wadhwa
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismrattaj
 
Chapter 12
Chapter 12Chapter 12
Chapter 12tp3115
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritanceshatha00
 

Destacado (20)

Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
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
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPT
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Inheritance
InheritanceInheritance
Inheritance
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
 

Similar a Constructor and Destructor Guide in C

chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdfstudy material
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptxRassjb
 
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, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxDeepasCSE
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfLadallaRajKumar
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++RAJ KUMAR
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and DestructorsKeyur Vadodariya
 
C++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming ConceptsC++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming Conceptsdharawagh9999
 
Tutconstructordes
TutconstructordesTutconstructordes
TutconstructordesNiti Arora
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdfMadnessKnight
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++sandeep54552
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctorSomnath Kulkarni
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 

Similar a Constructor and Destructor Guide in C (20)

chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
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++
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
C++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming ConceptsC++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming Concepts
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
 
Constructor
ConstructorConstructor
Constructor
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 

Más de Kamal Acharya

Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computerKamal Acharya
 
Introduction to Computer Security
Introduction to Computer SecurityIntroduction to Computer Security
Introduction to Computer SecurityKamal Acharya
 
Making decision and repeating in PHP
Making decision and repeating  in PHPMaking decision and repeating  in PHP
Making decision and repeating in PHPKamal Acharya
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in phpKamal Acharya
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPKamal Acharya
 
Capacity Planning of Data Warehousing
Capacity Planning of Data WarehousingCapacity Planning of Data Warehousing
Capacity Planning of Data WarehousingKamal Acharya
 
Information Privacy and Data Mining
Information Privacy and Data MiningInformation Privacy and Data Mining
Information Privacy and Data MiningKamal Acharya
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data MiningKamal Acharya
 
Classification techniques in data mining
Classification techniques in data miningClassification techniques in data mining
Classification techniques in data miningKamal Acharya
 
Introduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingIntroduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingKamal Acharya
 

Más de Kamal Acharya (20)

Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computer
 
Computer Arithmetic
Computer ArithmeticComputer Arithmetic
Computer Arithmetic
 
Introduction to Computer Security
Introduction to Computer SecurityIntroduction to Computer Security
Introduction to Computer Security
 
Session and Cookies
Session and CookiesSession and Cookies
Session and Cookies
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
Web forms in php
Web forms in phpWeb forms in php
Web forms in php
 
Making decision and repeating in PHP
Making decision and repeating  in PHPMaking decision and repeating  in PHP
Making decision and repeating in PHP
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in php
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHP
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Capacity Planning of Data Warehousing
Capacity Planning of Data WarehousingCapacity Planning of Data Warehousing
Capacity Planning of Data Warehousing
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Search Engines
Search EnginesSearch Engines
Search Engines
 
Web Mining
Web MiningWeb Mining
Web Mining
 
Information Privacy and Data Mining
Information Privacy and Data MiningInformation Privacy and Data Mining
Information Privacy and Data Mining
 
Cluster Analysis
Cluster AnalysisCluster Analysis
Cluster Analysis
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data Mining
 
Classification techniques in data mining
Classification techniques in data miningClassification techniques in data mining
Classification techniques in data mining
 
Data Preprocessing
Data PreprocessingData Preprocessing
Data Preprocessing
 
Introduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingIntroduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data Warehousing
 

Último

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 

Último (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

Constructor and Destructor Guide in C

  • 1. Constructor and Destructor 1 . C O N S T R U C TO R 2. DEFAULT CONSTRUCTOR 3. PARAMETERIZED CONSTRUCTOR 4. COPY CONSTRUCTOR 5. OVERLOADING CONSTRUCTOR 6 . D E S T R U C T OR Compiled By: Kamal Acharya
  • 2. Constructor  Special member function to initialize the objects of its class.  Its name is same as the class name.  It is invoked whenever the object is created.  It construct the values of data members of the class so it is named as the constructor. Compiled By: Kamal Acharya
  • 3.  Example Class integer { int m,n; public: integer(); // constructor declared ……… }; Compiled By: Kamal Acharya Constructor defination integer:: integer() { m=0; n=0; }
  • 4.  When object is created from class from class integer, it will be initialized automatically.  Eg. integer int1;  Here not only int1 is created but also its data members m and n are initialized to zero. Compiled By: Kamal Acharya
  • 5. Characteristics of Constructor  They should be declared in the public section.  They are called automatically when the object are created.  They do not have return type even void.  They have same name as the class name.  They can have default arguments as the other function. Compiled By: Kamal Acharya
  • 6. Default Constructor  They takes no parameters.  They are called internally by the compiler whenever the object are created.  There is no need to call it explicitly. Compiled By: Kamal Acharya
  • 7. Sample Program #include<iostream.h> #include<conio.h> class integer { int m,n; public: integer() { m=0; n=0; } Compiled By: Kamal Acharya void display() { cout<<"m= "<<m<<" and n= "<<n; } }; void main() { clrscr();r integer int1; int1.display(); getch(); }
  • 9. Parameterized Constructor  These are the constructor that take arguments.  They initialized the object data members by the value which is passed as arguments.  They are invoked when we pass the arguments to the object when they are being defined.  Example: integer int1(2,5); Compiled By: Kamal Acharya
  • 10. Sample Program #include<iostream.h> #include<conio.h> class integer { int m,n; public: integer(int x, int y) { m=x; n=y; } Compiled By: Kamal Acharya void display() { cout<<"m= "<<m<<" and n= "<<n; } }; void main() { clrscr(); integer int1(5,6); int1.display(); getch(); }
  • 12. Copy Constructor  It is used to declare and initialized an object from another object.  It takes reference to an object of the same class as itself as an arguments.  Example: integer I1; integer I2(I1); Compiled By: Kamal Acharya
  • 13. Sample Program #include<iostream.h> #include<conio.h> class integer { int m,n; public: integer(integer &x) { m=x.m; n=x.n; } integer() { m=100; n=100; } void display() { cout<<"m= "<<m<<" and n= "<<n<<endl; } }; Compiled By: Kamal Acharya
  • 14. void main() { clrscr(); integer int1; int1.display(); integer int2(int1); int2.display(); getch(); } Compiled By: Kamal Acharya
  • 16. Overloading Constructor  Constructor overloading is the process of defining more than one constructor in the same class.  C++ permits us to use multiple constructor in the same class. Compiled By: Kamal Acharya
  • 17. Sample Program #include<iostream.h> #include<conio.h> class integer { int m,n; public: integer(integer &x) { m=x.m; n=x.n; } Compiled By: Kamal Acharya integer() { m=0; n=0; } integer(int x, int y) { m=x; n=y; }
  • 18. void display() { cout<<"m= "<<m<<" and n= "<<n<<endl; } }; Compiled By: Kamal Acharya void main() { clrscr(); integer int1; integer int2(400,500); integer int3(int2); int1.display(); int2.display(); int3.display(); getch(); }
  • 20. Destructor  It is used to destroy the objects created by the constructor.  It is called for the class object whenever it passes the scope in the program.  Whenever new is used in the constructor to allocate the memory delete should be used in the destructor to free the memory for future use. Compiled By: Kamal Acharya
  • 21. Characteristics of Destructor  It has same name as the class name but is preceded by tilde (~) sign.  It has no return type and do not take any arguments.  It can not be overloaded.  It is called whenever the object get out of its scope. Compiled By: Kamal Acharya
  • 22. Sample Program #include<iostream.h> class integer { int m,n; public: integer() { m=0; n=0; cout<<"Default Constructor is called"<<endl; Compiled By: Kamal Acharya } integer(int x, int y) { m=x; n=y; cout<<"Parameterize d Constructor is called"<<endl; } ~integer() { cout<<"Object is detroyed"<<endl; }
  • 23. void display() { cout<<"m= "<<m<<" and n= "<<n<<endl; } }; Compiled By: Kamal Acharya void main() { clrscr(); { integer int1; int1.display(); } { integer int2(2,5); int2.display(); } }